Java之extends关键字

extends关键字


1.Java中使用extends关键字实现类的集成机制

2.通过继承子类自动拥有了基类(superclass)的所有成员(成员变量和成员方法)

3.Java只支持单继承

4.一个子类只能有一个基类,一个基类可以派生出多个子类



calss Person {
	private String name;
	private int age;
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
	this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
}

calss Student extends Person {
	private String school;
	public String getSchool() {
		return school;
	}
	
	public void setSchool(String school) {
		this.school = school;
	}
}

public class TestPerson {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("john");
		student.setAge(18);
	}
}


你可能感兴趣的:(JAVA学习)