public class Test {
public static void main(String[] args) {
//new可以理解为将一个抽象的类给实例化为一个具体的对象
Person person = new Person();
//这个实例对象调用了自己的一个方法
person.sleep();
Person cxy = new NewStudent();
cxy.sleep();
Student student = new Student("小闫同学", 19, "通信工程");
NewStudent newStudent = new NewStudent("小陈同学", 18, "软件工程");
NewStudent anotherNewStudent = new NewStudent("小黄同学", 18, "软件工程");
newStudent.introduce();
newStudent.introduce("114514");
System.out.println(newStudent.number);
}
}
//这是一个Person类,由方法和域构成
class Person {
protected String name;
protected int age;
public void sleep() {
System.out.println("人是要睡觉的");
}
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
//这是一个接口,它定义了些还未实现的抽象方法
interface NewFresh {
void introduce();
}
//这个Student类继承了Person类,思考一下,继承意味着什么呢?
class Student extends Person {
static int number = 0;
protected String major;
public Student() {
number++;
}
public Student(String name, int age, String major) {
super(name, age);
number++;
this.major = major;
}
public void sleep() {
System.out.println("你这个阶段还能睡得着觉?");
}
}
//这个类不光继承了一个Student类,还实现了一个接口
class NewStudent extends Student implements NewFresh {
public NewStudent() {
}
public NewStudent(String name, int age, String major) {
super(name, age, major);
}
public void introduce() {
System.out.println(age + "岁 是学生");
}
public void introduce(String introduction) {
System.out.println(introduction);
}
}
求求各位大佬救救孩子