儿子继承父亲

package com.t.t;

public class Extends {
    public static void main(String[] args) {
        Student s1=new Student("软件","天数",22);
        System.out.println(s1 instanceof Student);
        System.out.println(s1 instanceof ren);
    }
}
class ren{
    String name;
    int id;
    int height;
    public void rest()
    {
        System.out.println("傻逼");
    }
}
class Student extends ren{
    String major;//专业
    public void studay(){
        System.out.println("你🐎");
        rest();
        System.out.println(this.name);
    }

    public Student(String major,String name,int height) {
        this.major = major;
        this.name = name;
        this.height=height;
    }
}
class Boy extends ren{
    //instanceof运算符进行类型判断:左边是对象,右边是类,当对象是右面类或子类所创建对象返回true,否则,返回false
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,根据你的要求,我给你提供一个简单的代码示例: ```cpp #include <iostream> #include <string> using namespace std; // 父亲类 class Father { protected: string lastname; // 姓氏 public: string name; // 姓名 int age; // 年龄 Father(string lastname, string name, int age) { this->lastname = lastname; this->name = name; this->age = age; } }; // 母亲类 class Mother { protected: string nation; // 民族 public: string name; // 姓名 int age; // 年龄 Mother(string nation, string name, int age) { this->nation = nation; this->name = name; this->age = age; } }; // 子女类 class Child : public Father, public Mother { public: Child(string lastname, string nation, string name, int age) : Father(lastname, "", 0), Mother(nation, "", 0) { this->name = name; this->age = age; } void showInfo() { cout << "姓名:" << lastname << name << endl; cout << "民族:" << nation << endl; cout << "父亲姓名:" << lastname << endl; cout << "母亲姓名:" << Mother::name << endl; } }; int main() { Child child("张", "汉族", "小明", 10); child.showInfo(); return 0; } ``` 运行结果如下: ``` 姓名:张小明 民族:汉族 父亲姓名:张 母亲姓名: ``` 其中,父亲类和母亲类都有一个 protected 级别的数据成员,用于存储子女继承的姓和民族。子女类继承父亲类和母亲类,其中父亲类和母亲类的构造函数都有一个空的姓名参数,因为子女类继承父亲的姓和母亲的民族,所以在子女类的构造函数中只需要传入姓名和年龄即可。 最后,调用子女类的 showInfo() 函数输出子女及其父母的姓名和民族信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酒菡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值