Java定义People类

Java定义People类,它具有以下成员变量:String name,int age,它有两个构造方法!

package testclass;

public class PeopleClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s = new Student("s1");
		s.work();
		Teacher t = new Teacher("t1");
		t.work();
	}
}
class People{
	private String name;
	private Integer age=20;
	
	public People(String name, Integer age){
		System.out.println("A1");
		this.name = name;
		this.age = age;
	}
	
	public People(String name){
		System.out.println("A2");
		this.name = name;
		new People(name, age);
	}
	
	public void work(){
		System.out.println("A work");
	}
}

class Teacher extends People{
	public Teacher(String name){
		super(name);
	}
	
	public void work(){
		System.out.println("T are teach");
	}
}

class Student extends People{
	public Student(String name){
		super(name);
	}
	
	public void work(){
		System.out.println("Student are teach");
	}
}
输出结果:
A2
A1
Student are teach
A2
A1
T are teach



你可能感兴趣的:(java编程知识)