面向对象第三天,一定要坚持住,明天休息好好复习一下
- class Demo1 {
- public static void main(String[] args) {
- Student s1 = new Student();
- s1.setName ("李金洋");
- s1.setAge (22);
- s1.study();
- System.out.println(s1.getName() + "..." + s1.getAge());
- System.out.println("------------------");
-
- Student s2 = new Student("赵文悦",23);
- System.out.println(s2.getName()+"...."+ s2.getAge());
- System.out.println("------------------");
- Teacher t = new Teacher("孙艳芬",36);
- System.out.println(t.getName() + "..." + t.getAge());
- t.teach();
- }
- }
- class Person {
- private String name;
- private int age;
-
- public Person() {}
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void eat() {
- System.out.println(name + "吃饭");
- }
-
- }
- class Student extends Person{
- public Student() {}
- public Student(String name,int age) {
- super(name,age);
- }
- public void study() {
- System.out.println(getName() + "学习");
- }
- }
- class Teacher extends Person {
- public Teacher() {}
- public Teacher(String name,int age) {
- super(name,age);
- }
- public void teach() {
- System.out.println(getName() + "讲课");
- }
- }
复制代码 |
|