zoukankan      html  css  js  c++  java
  • Java继承

    父类的继承只能是单继承

    父类的继承修饰符extends 

    public class Fu{  // 父类
      // 成员变量
        private int age;
       private String name; 
     // 构造方法
      public Fu(){}
     public Fu(int age,String name) {
        this.age = age;
        this.nmae = name;
      }
      
      // 成员方法
      public void eat(){
        System.out.println("睡觉");
      }
      
      public void showInfo(){
        System.out.println("姓名:"+name);
        System.out.printin("年龄:"+age);
      }
    }

      

    public class Zi extends Fu{  // 子类
      // 成员变量
      private boolean sex;
      // 构造方法
      public Zi(){super()};
      public Zi(int age, String name, boolean sex){
        super(age,name);
        this.sex = sex;
      }
      // 覆盖重写父类方法
      @override
      public void showInfo(){
        super.showInfo();
        System.out.println("性别:"+sex);
      }
    }

      

    public class Demo{
      public static void main(String[] args) {
            Zi obj = new Zi(18,"name"); 
         System.out.prinln(obj.age); // 18
         obj.showInfo();   } }

    抽象类: 格式 public abstract 类名{}

    注意事项:

        1、抽象类没有实例

        2、抽象类被继承之后要将抽象类中的所有抽象方法实现

        3、子类中有一个抽象方法没有实现那么该子类就必须是抽象类

        4、抽象类不一定有抽象方法,有抽象方法的类一定的是抽象类

         

  • 相关阅读:
    docker使用
    window版docker安装及配置
    mysql命令
    xshell
    git 命令
    分页器原理
    PCL-Kinfu编译手册
    cmake-add_definitions
    cmake-include_directories
    cmake-source_group
  • 原文地址:https://www.cnblogs.com/zbaby/p/12641825.html
Copyright © 2011-2022 走看看