Java inheritance继承类应用与子母类访问权限讨论

• Inheritance: is the process by which one new class, called the derived class, is created from another class, called the base class.

• The derived class is also called: subclass or  child class

• The base class is also called: superclass or parent class

• extends: To specify that Student is a derived class (subclass) of Person we add the descriptor “extends” to the class definition:

• public class Student extends Person { … }

• Notice that a Student class

• Inherits everything from the Person class

• A Student IS-A Person (wherever a Person is needed, we can use a Student).

• super( ): When initializing a new Student object, we need to initialize its base class (or superclass). This is done by calling super( … ). For example, super(name, id) invokes the constructor Person(name, id)

• super( … ) must be the first statement of your constructor

• If you do not call super( ), Java will automatically invoke the base class’s default constructor

• What if the base class’s default constructor is undefined? Error

• You must use “super( … )”, not “Person( … )”.

Example:

superclass:

public class Person {
    private String name;
    private int id;

    public Person(String name, int id) {
	this.name = name;
	this.id = id;
    }

    public Person() {
	this("Unknown", 0);
    }

    public Person(Person person) {
	this(person.name, person.id);
    }

    public String getName() {
	return name;
    }

    public int getId() {
	return id;
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String toString() {
	return "[" + name + "] " + id;
    }

    public boolean equals(Object obj) {
	if (obj == this)
	    return true;

	if (!(obj instanceof Person))
	    return false;

	Person person = (Person) obj;

	return name.equals(person.name);
    }

    public int hashCode() {
	return id;
    }
    
    public static void main(String[] args) {
	Person p1 = new Person("Paul", 10);
	Person p2 = new Person("Mary", 20);
	Person p3 = new Person(p2);

	System.out.println(p1);
	System.out.println("Same?: " + p1.equals(p2));
	System.out.println("Same?: " + p2.equals(p3));
    }
}
subclass:
public class Student extends Person {
    private int admitYear;
    private double gpa;

    public Student(String name, int id, int admitYear, double gpa) {
 	super(name, id); /* calls super class constructor */
 	this.admitYear = admitYear;
 	this.gpa = gpa;
     }
    
    /* What would happen if we remove the Person default constructor? */
    public Student() {
	super();	/* calls base case constructor (what if we remove it?) */
	admitYear = -1;
	gpa = 0.0;
    }

    public Student(Student s) {
	super(s);       /* calls super class copy constructor */
	admitYear = s.admitYear;
	gpa = s.gpa;
    }

    public int getAdmitYear() {
	return admitYear;
    }

    public double getGpa() {
	return gpa;
    }

    public void setAdmitYear(int admitYear) {
	this.admitYear = admitYear;
    }

    public void setGpa(double gpa) {
	this.gpa = gpa;
    }

    public String toString() {
	/* Using super to call super class method */
	return super.toString() + " " + admitYear + " " + gpa;
    }

    public boolean equals(Object obj) {
	if (obj == this)
	    return true;

	if (!(obj instanceof Student))
	    return false;

	Student student = (Student) obj;

	/* Relying on Person equals; passing student */
	return super.equals(student) && admitYear == student.admitYear;
    }
    
    public static void main(String[] args) {
	Student bob = new Student("Bob", 457, 2004, 4.0);
	Student robert = new Student(bob);
	Student tom = new Student("Tom", 457, 2004, 4.0);
	
	System.out.println(bob);
	System.out.println("Same:" + bob.equals(robert));
	System.out.println("Same:" + tom.equals(robert));
    }
}
public class Faculty extends Person {
    private int hireYear; /* year when hired */

    public Faculty(String name, int id, int hireYear) {
	super(name, id);
	this.hireYear = hireYear;
    }
    
    public Faculty() {
	super();
	hireYear = -1;
    }

    public Faculty(Faculty faculty) {
	/* Why are we using get methods for the first two ? */
	this(faculty.getName(), faculty.getId(), faculty.hireYear);
    }

    int getHireYear() {
	return hireYear;
    }

    void setHireYear(int year) {
	hireYear = year;
    }

    public String toString() {
	return super.toString() + " " + hireYear;
    }

    public boolean equals(Object obj) {
	if (obj == this)
	    return true;

	if (!(obj instanceof Faculty))
	    return false;

	Faculty faculty = (Faculty) obj;

	/* Relying on Person equals; passing student */
	return super.equals(faculty) && hireYear == faculty.hireYear;
    }
    
    public static void main(String[] args) {
	Faculty john = new Faculty("John", 20, 2004);
	Faculty jack = new Faculty(john);
	Faculty mary = new Faculty("Mary", 101, 2010);
	
	System.out.println(mary);
	System.out.println("Same:" + john.equals(jack));
	System.out.println("Same:" + jack.equals(mary));
	
	System.out.println("Id: " + john.getId());
	System.out.println("HireYear: " + john.getHireYear());
    }
}

子母不同类型访问权限一览:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值