码迷,mamicode.com
首页 > 其他好文 > 详细

继承分析

时间:2019-02-19 18:53:50      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:end   set   5.5   分析   nbsp   lis   new   int   style   

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 + "\n" +
        "addr = " + this.addr + "\n" +
        "sex = " + this.sex + "\n" +
        "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() + "\n" +
        "addr = " + super.getAddr() + "\n" +
        "sex = " + super.getSex() + "\n" +
        "age = " + super.getAge() + "\n" +
        "math = " + this.math + "\n" +
        "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 + "\n" +
        "age = " + this.age + "\n" +
        "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() + "\n" +
        "age = " + super.getAge() + "\n" +
        "sex = " + super.getSex() + "\n" +
        "job = " + this.job + "\n" +
        "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() + "\n" +
        "age = " + super.getAge() + "\n" +
        "sex = " + super.getSex() + "\n" +
        "dname = " + this.dname + "\n" +
        "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>*/

 

继承分析

标签:end   set   5.5   分析   nbsp   lis   new   int   style   

原文地址:https://www.cnblogs.com/dododo70/p/10402856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!