C++ 模板函数纯虚函数和Java对比

多态(虚函数)。 动态多态(程序的角度上:程序在运行期间才能确定调用哪个类的函数 == 动态多态的范畴)

Java语言默认支持多态
C++默认关闭多态,怎么开启多态? 虚函数 在父类上给函数增加 virtual关键字

#include <iostream>
using namespace std;

// Android标准
class BaseActivity {
public:
     virtual void onStart() {
        cout << "BaseActivity onStart" << endl;
    }
};

class HomeActivity : public BaseActivity {
public:
    void onStart() { // 重写父类的函数
        cout << "HomeActivity onStart" << endl;
    }
};

class LoginActivity : public BaseActivity {
public:
    void onStart() { // 重写父类的函数
        cout << "LoginActivity onStart" << endl;
    }
};

// 在此函数 体系多态,例如:你传入HomeActivity,我就帮你运行HomeActivity
void startToActivity(BaseActivity * baseActivity) {
    baseActivity->onStart();
}

int main() {
    // TODO 第一版本
    HomeActivity *homeActivity = new HomeActivity();
    LoginActivity *loginActivity = new LoginActivity();

    startToActivity(homeActivity);
    startToActivity(loginActivity);

    if (homeActivity && loginActivity) delete homeActivity; delete loginActivity;


    cout << endl;


    // TODO 第二个版本
    BaseActivity * activity1 = new HomeActivity();
    BaseActivity * activity2 = new LoginActivity();
    startToActivity(activity1);
    startToActivity(activity2);


    // TODO 抛开 C++ 抛开Java 等等,请问什么是多态? 父类的引用指向之类的对象,同一个方法有不同的实现,重写(动态多态)和   重载(静态多态)

    return 0;
}

静态多态 (编译期已经决定,调用哪个函数了,这个就属于静态多态的范畴) 重载(静态多态)

#include <iostream>

using namespace std;

void add(int number1, int number2) {
    cout << number1 + number2 << endl;
}

void add(float number1, float number2) {
    cout << number1 + number2 << endl;
}

void add(double number1, double number2) {
    cout << number1 + number2 << endl;
}

int main() {
    add(10000, 10000);
    add(1.9f, 2.8f);
    add(545.4, 654.54);

    return 0;
}

纯虚函数(Java版抽象类)

// C++纯虚函数(C++没有抽象类)  相当于 Java的抽象类  为了更好理解

#include <iostream>
using namespace std;

// 抽象类/纯虚函数: 分为:1.普通函数, 2.抽象函数/纯虚函数
class BaseActivity {
private:
    void setContentView(string layoutResID) {
        cout << "XmlResourceParser解析布局文件信息... 反射" << endl;
    }

public:
    // 1.普通函数
    void onCreate() {
        setContentView(getLayoutID());

        initView();
        initData();
        initListener();
    }

    // 纯虚函数是必须继承的(如果子类没有重写纯虚函数,子类就是抽象类), 虚函数是不是不必须的

    // 2.抽象函数/纯虚函数
    // virtual string getLayoutID(); // 虚函数
    virtual string getLayoutID() = 0; // 纯虚函数
    virtual void initView() = 0;
    virtual void initData() = 0;
    virtual void initListener() = 0;
};

// 子类 MainActivity
class MainActivity : public BaseActivity { // MainActivity如果没有重新父类的纯虚函数,自己就相当于 抽象类了

    string getLayoutID() {
        return "R.layout.activity_main";
    }

    void initView()  {
        // Button btLogin = findViewById(R.id.bt_login);
        // Button btRegister = findViewById(R.id.bt_register);
        // TextView tvInfo = findViewById(R.id.tv_info);
        // ... 省略
    }

    void initData() {
        // tvInfo.setText("info...");
        // ... 省略
    }

    void initListener() {
        /*btLogin.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       // 点击做事情
                   }
               });*/
        // ... 省略
    }
};

// 子类 HomeActivity
class HomeActivity : public BaseActivity { // MainActivity如果没有重新父类的纯虚函数,自己就相当于 抽象类了

    string getLayoutID() {
        return "R.layout.activity_home";
    }

    void initView()  {
        // Button btLogin = findViewById(R.id.bt_login);
        // Button btRegister = findViewById(R.id.bt_register);
        // TextView tvInfo = findViewById(R.id.tv_info);
        // ... 省略
    }

    void initData() {
        // tvInfo.setText("info...");
        // ... 省略
    }

    void initListener() {
        /*btLogin.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       // 点击做事情
                   }
               });*/
        // ... 省略
    }
};

// 子类 LoginActivity
class LoginActivity : public BaseActivity { // MainActivity如果没有重新父类的纯虚函数,自己就相当于 抽象类了

    string getLayoutID() {
        return "R.layout.activity_login";
    }

    void initView()  {
        // Button btLogin = findViewById(R.id.bt_login);
        // Button btRegister = findViewById(R.id.bt_register);
        // TextView tvInfo = findViewById(R.id.tv_info);
        // ... 省略
    }

    void initData() {
        // tvInfo.setText("info...");
        // ... 省略
    }

    void initListener() {
        /*btLogin.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       // 点击做事情
                   }
               });*/
        // ... 省略
    }
};

int main() {

    // 错误:抽象类型 MainActivity 绝对不能实例化
    // MainActivity mainActivity;

    // 重新了父类所有的纯虚函数
    MainActivity mainActivity;

    return 0;
}

虚函数 纯虚函数 全纯虚函数(C++没有接口) 等价于 6.全纯虚函数(Java版接口)。

#include <iostream>
using namespace std;

class Student {
    int _id;
    string name;
    int age;
};

// 此类所有的函数 ,都是纯虚函数,就相当于 Java的接口了
class ISudent_DB {
    virtual void insertStudent(Student student) = 0;
    virtual void deleteStudent(int _id) = 0;
    virtual void updateStudent(int _id, Student student) = 0;
    virtual Student queryByStudent(Student student) = 0;
};

// Java的实现类
class Student_DBImpl1 : public ISudent_DB {
public:
    void insertStudent(Student student) {
        // 插入操作,省略代码...
    }

    void deleteStudent(int _id) {
        // 删除操作,省略代码...
    }

    void updateStudent(int _id, Student student) {
        // 更新操作,省略代码...
    }

    Student queryByStudent(Student student) {
        // 查询操作,省略代码...
    }
};

// Java的实现类
class Student_DBImpl2 : public ISudent_DB {
public:
    void insertStudent(Student student) {
        // 插入操作,省略代码...
    }

    void deleteStudent(int _id) {
        // 删除操作,省略代码...
    }

    void updateStudent(int _id, Student student) {
        // 更新操作,省略代码...
    }

    Student queryByStudent(Student student) {
        // 查询操作,省略代码...
    }
};

// Java的实现类
class Student_DBImpl3 : public ISudent_DB {
public:
    void insertStudent(Student student) {
        // 插入操作,省略代码...
    }

    void deleteStudent(int _id) {
        // 删除操作,省略代码...
    }

    void updateStudent(int _id, Student student) {
        // 更新操作,省略代码...
    }

    Student queryByStudent(Student student) {
        // 查询操作,省略代码...
    }
};

int main() {
    Student_DBImpl1 studentDbImpl1;
    Student_DBImpl2 studentDbImpl2;
    Student_DBImpl3 studentDbImpl3;

    cout << "Success" << endl;

    return 0;
}

模版函数(Java版泛型)。 C++没有泛型 C++的模板函数 非常类似于 Java的泛型

#include <iostream>

using namespace std;

// 加分合集  int double float ... 你都要考虑,你是不是要定义很多的 函数
/*void addAction(int n1, int n2) {
    cout << "addAction(int n1, int n2):" << n1 + n1 << endl;
}

void addAction(float n1, float n2) {
    cout << "addAction(int n1, int n2):" << n1 + n1 << endl;
}

void addAction(double n1, double n2) {
    cout << "addAction(int n1, int n2):" << n1 + n1 << endl;
}*/

// 模板函数  == Java的泛型解决此问题
template <typename TT>
void addAction(TT n1, TT n2) {
    cout << "模板函数:" << n1 + n2 << endl;
}

int main() {
    addAction(1, 2);
    addAction(10.2f, 20.3f);
    addAction(545.34, 324.3);
    addAction<string>("AAA", "BBB");

    /*addAction(2, 324.3);
    addAction(54, 324.3f);*/
    return 0;
}

C++ 回调写法类比Java

//回调。  Java的登录 简单的 接口

#include <iostream>
using namespace std;

// 登录成功的Bean
class SuccessBean {
public:
    string username;
    string userpwd;

    SuccessBean(string username, string userpwd)
    :username(username), userpwd(userpwd) {}
};

// 登录响应的接口  成功,错误
class ILoginResponse {
public:
    // 登录成功
    virtual void loginSuccess(int code, string message, SuccessBean successBean) = 0;

    // 登录失败
    virtual void loginError(int code, string message) = 0;
};

// 登录的API动作
void loginAction(string name, string pwd, ILoginResponse & loginResponse) {
    if (name.empty() || pwd.empty()) {
        cout << "用户名或密码为空!" << endl;
        return;
    }

    if ("ahehehehe" == name && "wdddd" == pwd) {
        loginResponse.loginSuccess(200, "登录成功", SuccessBean(name, "恭喜你进入"));
    } else {
        loginResponse.loginError(404, "登录错误,用户名或密码错误...");
    }
}

// 写一个实现类,继承接口
// 接口实现类
class ILoginResponseImpl : public ILoginResponse {
public:
    // 登录成功
    void loginSuccess(int code, string message, SuccessBean successBean) {
        cout << "恭喜登录成功 " << "code:" << code << " message:" << message
        << "successBean:" << successBean.username << "," << successBean.userpwd << endl;
    }

    // 登录失败
    void loginError(int code, string message) {
        cout << " 登录失败 " << "code:" << code << " message:" << message << endl;
    }
};

int main() {

    // 做实验
    // Allocating an object of abstract class type 'ILoginResponse'
    // 正在分配抽象类型为ILoginResponse的对象  不能被实例化
    // 纠结:为什么不可以
    // 1.他不是Java的接口,C++也没有接口,他只是像接口而已。
    // 2.他也不是抽象类,C++也没有抽象类,他只是像抽象类而已。
    // 3.他是纯虚函数的类,此类决定不准你实例化  无论堆区 还是栈区
    /*new ILoginResponse() {
        // 登录成功
        void loginSuccess(int code, string message, SuccessBean successBean) {

        }

        // 登录失败
        void loginError(int code, string message) {

        }
    }*/

    string username;
    cout << "请输入用户名.." << endl;
    cin >> username;

    string userpwd;
    cout << "请输入密码.." << endl;
    cin >> userpwd;

    ILoginResponseImpl iLoginResponse;
    loginAction(username, userpwd, iLoginResponse);

    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,682评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,277评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,083评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,763评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,785评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,624评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,358评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,261评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,722评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,030评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,737评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,360评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,941评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,057评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,237评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,976评论 2 355

推荐阅读更多精彩内容