继承分析

Posted dododo70

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承分析相关的知识,希望对你有一定的参考价值。

class Person {
    //定义成员变量
    private String name;
    private String addr;
    private char sex;
    private int age;
    
    //setter,getter方法
    public void setName(String name){
        this.name = name;
    }
    public void setAddr(String addr){
        this.addr = addr;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public void setAge(int age){
        this.age = age;
    }
    
    public String getName(){
        return this.name;
    }
    public String getAddr(){
        return this.addr;
    }
    public char getSex(){
        return this.sex;
    }
    public int getAge(){
        return this.age;
    }
    
    //构造方法
    public Person(){}
    public Person(String name,String addr){
        this();
        this.name = name;
        this.addr = addr;
    }
    public Person(String name, String addr, char sex, int age){
        this(name,addr);
        this.sex = sex;
        this.age = age;
    }
    
    //输出数据
    public String getInfo(String name, String addr, char sex, int age){
        return 
        "name = " + this.name + "
" +
        "addr = " + this.addr + "
" +
        "sex = " + this.sex + "
" +
        "age = " + this.age;
    }
    
}

class Student extends Person {
    //补充新的成员变量
    private double math;
    private double english;
    
    //新成员变量的 setter,getter方法
    public void setMath(double math){
        this.math = math;
    }
    public void setEnglish(double english){
        this.english = english;
    }
    public double getMath(){
        return this.math;
    }
    public double getEnglish(){
        return this.english;
    }
    
    //子类的构造方法
    public Student(){}
    public Student(String name, String addr){
        super(name,addr);
    }
    public Student(String name, String addr, char sex, int age, double math, double english){
        super(name,addr,sex,age);
        this.math = math;
        this.english = english;
    }
    
    public String getInfo(){
        return 
        "name = " + super.getName() + "
" +
        "addr = " + super.getAddr() + "
" +
        "sex = " + super.getSex() + "
" +
        "age = " + super.getAge() + "
" +
        "math = " + this.math + "
" +
        "english = " + this.english;
    }
}

public class Demo{
    public static void main(String args[]){
        Student std = new Student("Allen","London",‘女‘,19,78.9,95.5);
        System.out.println(std.getInfo());
    }
}
/*------------------------------------------------------------
E:Javacode>java Demo
name = Allen
addr = London
sex = 女
age = 19
math = 78.9
english = 95.5

E:Javacode>*/
class Emp{
    private String name;
    private int age;
    private char sex;
    
    //setter,getter方法
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public char getSex(){
        return this.sex;
    }
    
    public Emp(){}
    public Emp(String name, int age, char sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    public String getInfo(){
        return
        "name = " + this.name + "
" +
        "age = " + this.age + "
" +
        "sex = " + this.sex;
    }
}

class Mgr extends Emp{
    private String job;
    private double income;
    
    public void setJob(String job){
        this.job = job;
    }
    public void setIncome(double income){
        this.income = income;
    }
    public String getJob(){
        return this.job;
    }
    public double getIncome(){
        return this.income;
    }
    
    public Mgr(){}
    public Mgr(String name, int age, char sex, String job, double income){
        super(name,age,sex);
        this.job = job;
        this.income = income;
    }
    
    public String getInfo(){
        return 
        "name = " + super.getName() + "
" +
        "age = " + super.getAge() + "
" +
        "sex = " + super.getSex() + "
" +
        "job = " + this.job + "
" +
        "income = " + this.income;
    }
    
}

class Staff extends Emp{
    private String dname;
    private double sal;
    
    public void setDname(String dname){
        this.dname = dname;
    }
    public void setSal(double sal){
        this.sal = sal;
    }
    public String getDname(){
        return this.dname;
    }
    public double getSal(){
        return this.sal;
    }
    
    public Staff(){}
    public Staff(String name, int age, char sex, String dname, double sal){
        super(name,age,sex);
        this.dname = dname;
        this.sal = sal;
    }
    
    public String getInfo(){
        return 
        "name = " + super.getName() + "
" +
        "age = " + super.getAge() + "
" +
        "sex = " + super.getSex() + "
" +
        "dname = " + this.dname + "
" +
        "sal = " + this.sal;
    }
}

public class Demo{
    public static void main(String args[]){
        
        System.out.println("***********************");
        Staff stf1 = new Staff("AA",18,‘女‘,"销售部",3500.0);
        System.out.println(stf1.getInfo());
        
        System.out.println("***********************");
        Mgr mgr1 = new Mgr("BB",45,‘男‘,"销售总监",35000.0);
        System.out.println(mgr1.getInfo());
    }
}
/*-------------------------------------------------------
E:Javacode>java Demo
***********************
name = AA
age = 18
sex = 女
dname = 销售部
sal = 3500.0
***********************
name = BB
age = 45
sex = 男
job = 销售总监
income = 35000.0

E:Javacode>*/

 

以上是关于继承分析的主要内容,如果未能解决你的问题,请参考以下文章

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段

操作 Java 泛型:泛型在继承方面体现与通配符使用

26.Qt Quick QML-RotationAnimationPathAnimationSmoothedAnimationBehaviorPauseAnimationSequential(代码片段

CPNtools协议建模安全分析---实例变迁标记

优化 C# 代码片段、ObservableCollection 和 AddRange