继承

继承

1.继承关系有两个类,父类和子类。子类是父类的拓展。

2.一个子类只能有一个父类,一个父类可以有多个子类。

父类:

package com.tiedandan.oop.继承;

public class Father {
  public int getAge() {
      return age;
  }

  public void setAge(int age) {
      this.age = age;
  }

  public int getHeight() {
      return height;
  }

  public void setHeight(int height) {
      this.height = height;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  private String name;
  private int age;
  private int height;
  public void give(){
      System.out.println("father is father");
  }
}

子类:

package com.tiedandan.oop.继承;

public class Person extends Father{
      private int age;

  @Override
  public int getAge() {
      return age;
  }

  @Override
  public void setAge(int age) {
      this.age = age;
  }

  @Override
  public int getHeight() {
      return height;
  }

  @Override
  public void setHeight(int height) {
      this.height = height;
  }

  private int height;
      public void let(){
          System.out.println("son is young son");
      }
}

测试类:

package com.tiedandan.oop.继承;

public class application {
  public static void main(String[] args) {
      Person person=new Person();

      person.setAge(18);
      person.setHeight(175);
      person.setName("213");
      System.out.println("父亲信息:");
      System.out.println(person.getName());
      Father father = new Father();
      System.out.println(father.getName());
      System.out.println(father.getAge());
      System.out.println(father.getHeight());
      System.out.println("儿子信息:");
      System.out.println(person.getAge());
      System.out.println(person.getHeight());

  }
}

 

posted on 2021-11-12 17:10  张铁蛋666  阅读(22)  评论(0编辑  收藏  举报

导航