Java面向对象特性

继承

package girlfriend;

/*final*/ class Creature {  //定义生物类

//final 关键字声明类可以把类定义为不能继承的,即最终类;
//或者用于修饰方法,该方法不能被子类重写:

    //定义属性


    int biological_energy; 


    String shape;


    //定义构造器


    public Creature(int biological_energy,String shape){


        this.biological_energy = biological_energy;


        this.shape = shape;        //this关键字区别成员变量和形参


    }


    //定义方法


    public /*final*/ void getpower(){


      System.out.println("Creature get power from solar power");


    }


}


class Rose extends Creature{  //定义玫瑰类


    //扩展属性


    String smell;


    String color;


    //定义构造器


    public Rose(int biological_energy,String shape,String smell,String color){


        super(85,"complicated");
        //显式地通过super关键字调用父类的构造器并配以适当的参数列表,默认调用super();


        this.biological_energy = biological_energy;


        this.shape = shape;


        this.smell = smell;


        this.color = color;


    }


    //定义方法(重写方法)


    public void getpower(){


        System.out.println("Rose get power from water,sunshine,soil...");


    }


    public void getTest(){


          //this'调用自己的方法


              this.getpower();


          //super调用父类的方法


            super.getpower();


    }


}
public class grilfriend 
{
public static void main(String[] args)
{


         Creature creature = new Creature(85,"complicated");


         creature.getpower();


         Rose rose = new Rose(56,"magnificant","Fangrance","red and white");


         rose.getTest();


     }
}

抽象



abstract class Person
{
    //定义属性
    public String name;
    public int age;
    public char gender;
    //定义方法(只允许声明,不允许实现)
    public abstract void setname(String name);
    public abstract void setage(int age);
    public abstract void showinfo();
}


public class Abstract extends Person
{
//重写方法
public void setname(String name)
{
this.name=name;
}
public void setage(int age)
{
     this.age=age;
}


public void showinfo()
{
       System.out.println(name);
       System.out.println(age);
}
public static void main(String []args)
{


        Abstract s = new Abstract();


        s.setname("lili");


        s.setage(12);


        s.showinfo();


    }
}

接口

interface Animal
{
    //接口没有构造方法
    void eat();
    void sleep();
}
interface Creature
{
    void getpower();
    void outpower();
}
interface Cat extends Animal,Creature
{
    void run();
    void voice();
}
public class Interface implements Cat  
//一个类可以继承多个接口,但接口的每个方法都得实现,除非是抽象类
{
    public void eat(){
        System.out.println("Animal need to eat");
    }
    public void sleep(){
        System.out.println("Animal need to sleep");
    }
    public void getpower(){
          System.out.println("Crature need to getpower");
    }
    public void outpower(){
          System.out.println("Crature need to outpower");
    }
    public void run(){
        System.out.println("Cat can run");
    }
    public void voice(){
        System.out.println("Cat can voice");
    }
    public static void main(String []args){
        Interface bofi = new Interface();


        bofi.eat();


        bofi.sleep();


        bofi.getpower();


        bofi.outpower();


        bofi.run();


        bofi.voice();
    }
}



猜你喜欢

转载自blog.csdn.net/qq_40754146/article/details/80709324