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();
}
}
作者:陈太浪
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。