java-抽象类和继承抽象类

抽象类和继承抽象类

抽象类不能直接实例化,需要创建一个指向自己的对象引用(其子类)来实例化
代码:

public class Main
{
    public static void main(String[] args)
    {
        /* People people = new People(); */ //错误
        People p = new Man();
        p.eat();
        p.drink();
        /* p.palyGames() */ //此方法错误
        Man man = new Man();
        man.eat();
        man.playGames();
        Women women = new Women();
        women.eat();
        women.dressing();
    }
    abstract static class People{
        String name;
        int age;
        float height;
        float weight;
        public void eat(){
            System.out.println("人都会吃饭");
        }
        public abstract void drink();
    }
    static class Man extends People{
        float short_hair;
        public void playGames(){
            System.out.println("男的独有的打游戏技能");
        }
        @Override
        public void drink(){
            System.out.println("man drink 酒");
        }
    }
    static class Women extends People{
        float long_hair;
        public void dressing(){
            System.out.println("女的独有的化妆技能");
        }
        @Override
        public void drink(){
            System.out.println("women drink 饮料");
        }
    }
}

这里People不能直接实例化,如果是People p = new Man();这里p只能调用People类n抽象方法和成果调用了其抽象方法,则访问的是其引用类(子类)继承的抽象类;如果是Man man= new Man();这里man是可以调用类Man和其继承的抽象类People的方法和属性
java-抽象类和继承抽象类_第1张图片

你可能感兴趣的:(java)