• 继承


    继承   是通过关键字 extends来实现的

    语法:子类  extends 父类{

                //类定义部分

              }

    下面来看一个例子:

    package jicheng;
    
    public class Pet {
       private int health;
       private String name;
       public Pet(){
           health=95;
       }
       public Pet(String name){
           this.name=name;
       }
    public int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        this.health = health;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    /**
     * 输出宠物信息
     */
    public void print(){
        System.out.println("宠物的自白:
    我的名字叫"+this.name+"我的健康值:"+this.health);
    }
    
       
       
    }
    package jicheng;
    
    public class Doog extends Pet{
          private String strain;
          public Doog(String name, String strain){
              super(name);
              this.strain=strain;
          }
          public String getStrain(){
              return strain;
          }
    }
    package jicheng;
    
    public class Penguin extends Pet{
        private String sex;
        public Penguin (String name,String sex){
            super(name);
            this.sex=sex;
            }
           public String getSex(){
               return sex;
        }
    }
    package jicheng;
    
    public class MyPestText {
        public static void main(String[] args) {
            Pet pet=new Pet("贝贝");
            pet.print();
            Doog dog=new Doog("狗狗","藏獒");
            dog.print();
            Penguin pen=new Penguin("bala","雄");
            pen.print();
        }
    }

    子类重写父类的方法

    package boke;
    
    public class Pet {
           private int health;
           private String name;
           public Pet(){
               health=95;
               name="ss";
           }
           public Pet(String name){
               this.name=name;
           }
        public int getHealth() {
            return health;
        }
        public void setHealth(int health) {
            this.health = health;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        /**
         * 输出宠物信息
         */
        public void print(){
            System.out.println("宠物的自白:
    我的名字叫"+this.name+"我的健康值:"+this.health);
        }
    
           
           
        }
    package boke;
    
    public class Dog extends Pet {
        /**
         * 狗狗类Pet的子类
         */
        public  String strain;
        /**
         * 有参构造方法
         */
        public Dog(String name ,String strain){
            //super(name);
            this.strain=strain;
        }
        public String getStrain(){
            return strain;
        }
        /**
         * 重写父类的方法
         */
        public void print(){
            super.print();
            System.out.println("我是一个"+this.strain);
        }
    }
    package boke;
    
    public class Penguin extends Pet {
       private String sex;//性别
       public Penguin(String name,String sex){
           super(name);
           this.sex=sex;
       }
       public String getSex(){
           return sex;
       }
       /**
        * 重写父类的print方法
        */
       public void print(){
           super.print();
           System.out.println("性别是:"+this.sex);
       }
    }
    package boke;
    
    public class Test {
        public static void main(String[] args) {
            Pet pet=new Pet("贝贝");
            pet.print();
            Dog dog=new Dog("狗狗","藏獒");
            dog.print();
            Penguin pen=new Penguin("bala","雄");
            pen.print();
        }
    }

    继承关系中的构造方法

    package boke;
    public class Person {
          String name;
          public Person(){
              System.out.println("execue Person()");
          }
          public Person(String name){
              this.name=name;
              System.out.println("execue Person(name)");
          }
    }
    package boke;
    
    public class Student extends Person{
          public String school;
         public Student(){
             System.out.println("execue Person(name)");
         }
         public Student(String name,String school){
             super(name);
             this.school=school;
         }
    }
    package boke;
    
    public class PostGraduate  extends Student {
         String guide;
         public PostGraduate(){
             System.out.println("execue PostGraduate ");
         }
         public PostGraduate (String name,String school,String guide){
             super(name,school);
             this.guide=guide;
             System.out.println("execue  PostGraduate( name,School, guide) ");
         }
         
    }
    package boke;
    
    public class Testi {
        public static void main(String[] args) {
            PostGraduate pg=null;
            pg=new PostGraduate();
            System.out.println();
            pg=new PostGraduate( "留","下","来");
        }
    }
  • 相关阅读:
    反向映射和写时复制
    内存分析的好blog
    minicom使用
    tee的妙用
    网络带宽
    mem analyse
    linux 应用层常用API/命令
    ubuntu 库依赖问题
    Python基础学习笔记(一:hello world)
    第7章 取消与关闭
  • 原文地址:https://www.cnblogs.com/cuixiaomeng/p/6748328.html
Copyright © 2020-2023  润新知