java中final、super、static关键字的使用


// 人类
class Person {
	
	public String name;
	public String gender;
	public int age;
	// final关键字的使用
	public final String school = "hbuas";
	// static关键字的使用
	public static String department;
	public void say() {
		System.out.println("Hello!");
	}
	
	public void show() {
		System.out.println("name: " + name);
		System.out.println("gender: " + gender);
		System.out.println("age: " + age);
	}
	
}

// 学生类
class Student extends Person {
	
	public int grade;
	Student(String name, String gender, int age, int grade) {
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.grade = grade;
	}
	
 	public void study() {
		System.out.println("good good learn!");
	}
	
	public void show() {
		
		// super关键字的使用
		super.show();
		System.out.println("I am a student.");
	}
	
	
}

// 教师类
class Teacher extends Person {
	
	public String course;
	
	Teacher(String name, String gender, int age, String course) {
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.course = course;
	}
	
	public void teach() {
		
		System.out.println("good good work!");
	}
	
	public void show() {
		
		// super关键字的使用
		// 调用父类中的show方法
		super.show();
		System.out.println("I am a teacher.");
	}
}

// 测试类
class demo {
	
	public static void main (String []args) {
		
		Student s = new Student("Tom", "male", 20, 2);
		// 为静态变量赋值
		s.department = "computer";
		System.out.println("school: " + s.school);
		System.out.println("department: " + s.department);
		s.show();
		s.study();
	
		System.out.println("——————————————————————————————");
		
		Teacher t = new Teacher("Wang", "male", 20, "math");
		System.out.println("school: " + t.school);
		// 直接输出静态变量
		System.out.println("department: " + t.department);
		t.show();
		t.teach();
	}
	
}


posted @ 2020-05-17 22:50  陈太浪  阅读(188)  评论(0编辑  收藏  举报