Java类的使用

103 篇文章 0 订阅
12 篇文章 0 订阅


继承:

Java中类只允许单继承,即一个子类只能有一个父类

Java中继承父类使用extends关键字

Java中构造方法不会继承,子类实例使用this指向自己,使用super指代其父类

Java中子类方法重写(override)父类方法时,对于异常检查而言,不能比父类方法抛出更宽泛的异常

 

抽象类:

Java中的抽象类不能实例化对象

Java中使用abstract class来声明定义抽象类

Java中可以使用abstract声明抽象方法,抽象方法没有方法定义,抽象方法必须在子类中实现(除非该子类也是抽象类),包含抽象方法的类必须的抽象类,构造方法不能为抽象方法

 

示例:

//Animal.java:

abstract public class Animal {

    public void eat() {

        System.out.println(species() + " eat something");

    }

    

    public void drink() {

        System.out.println(species() + " drink");

    }

    

    abstract public String species();

}

 

//Livestock.java:

abstract public class Livestock extends Animal {    

    public void doSomeWork() {

        System.out.println("Livestock do some work");

    }

}

 

//Poultry.java:

abstract public class Poultry extends Animal {

}

 

//Cattle.java:

public class Cattle extends Livestock {

    public void doSomeWork() {

        System.out.println("Cattle help farming");

    }

    

    public String species() {

        return "Cattle";

    }

}

 

//Sheep.java:

public class Sheep extends Livestock {

    public void doSomeWork() {

        System.out.println("Sheep produce wool");

    }

    

    public String species() {

        return "Sheep";

    }

}

 

//Chicken.java:

public class Chicken extends Poultry {

    public String species() {

        return "Chicken";

    }

}

 

//Duck.java:

public class Duck extends Poultry {

    public String species() {

        return "Duck";

    }

}

 

//AnimalDemo.java

public class AnimalDemo {

    public static void main(String args[]) {

        Livestock[] livestocks = new Livestock[] {new Cattle(), new Sheep()};

        Poultry[] poultries = new Poultry[] {new Chicken(), new Duck()};

        Animal[] animals = new Animal[] {livestocks[0], livestocks[1], poultries[0], poultries[1]};

        for (Animal animal:animals) {

            animal.eat();

            animal.drink();

        }

        for (Livestock livestock:livestocks) {

            livestock.doSomeWork();

        }

    }

}

 

//output:

//Cattle eat something

//Cattle drink

//Sheep eat something

//Sheep drink

//Chicken eat something

//Chicken drink

//Duck eat something

//Duck drink

//Cattle help farming

//Sheep produce wool

 

接口:

Java中一个类可以实现多个接口,接口名之间用逗号分隔,实现接口使用关键字Implements

Java中接口支持多继承,接口继承接口依然使用extends关键字

Java中当一个非抽象类实现接口时,必须实现接口定义的所有方法

Java中接口没有构造方法,除了staticfinal成员变量外,不能包含其他类型成员变量

//Activity.java:

public interface Activity {

}

 

//Teach.java:

public interface Teach extends Activity {

    public void TeachMath(int hour);

    public void TeachChinese(int hour);

    public void TeachEnglish(int hour);

    public void TeachHistory(int hour);

}

 

//Study.java:

public interface Study extends Activity {

    public void StudyMath(int hour);

    public void StudyChinese(int hour);

    public void StudyEnglish(int hour);

    public void StudyHistory(int hour);

}

 

//Play.java:

public interface Play extends Activity {

    public void PlayVideoGame(int hour);

    public void ListenToMusic(int hour);

    public void WatchMovie(int hour);

}

 

//People.java:

public class People {

    private String name;

    private Integer age;

    private String career;

    

    public People(String nameInteger age) {

        this.name = name;

        this.age = age;

    }

    

    public String Name() {

        return this.name;

    }

    

    public Integer Age() {

        return this.age;

    }

    

    public void ChangeCareer(String newCareer) {

        this.career = newCareer;

    }

}

 

//Student.java:

public class Student extends People implements PlayStudy {

    public Student(String nameInteger age) {

        super(name, age);

        ChangeCareer("Student");

    }

    public void StudyMath(int hour) {

        System.out.println(String.format("%s will study math for %d hour(s)", Name(), hour));

    }

    public void StudyChinese(int hour) {

        System.out.println(String.format("%s will study Chinese for %d hour(s)", Name(), hour));

    }

    public void StudyEnglish(int hour) {

        System.out.println(String.format("%s will study English for %d hour(s)", Name(), hour));

    }

    public void StudyHistory(int hour) {

        System.out.println(String.format("%s will study history for %d hour(s)", Name(), hour));

    }

    public void PlayVideoGame(int hour) {

        System.out.println(String.format("%s has played video game for %d hour(s)", Name(), hour));

    }

    public void ListenToMusic(int hour) {

        System.out.println(String.format("%s has been listening to music for %d hour(s)", Name(), hour));

    }

    public void WatchMovie(int hour) {

        System.out.println(String.format("%s has been watching movies for %d hour(s)", Name(), hour));

    }

}

 

//Teacher.java

public class Teacher extends People implements TeachPlay {

    public Teacher(String nameInteger age) {

        super(name, age);

        ChangeCareer("Teacher");

    }

    public void TeachMath(int hour) {

        System.out.println(String.format("%s will give lessons on math for %d hour(s)", Name(), hour));

    }

    public void TeachChinese(int hour) {

        System.out.println(String.format("%s will give lessons on Chinese for %d hour(s)", Name(), hour));

    }

    public void TeachEnglish(int hour) {

        System.out.println(String.format("%s will give lessons on English for %d hour(s)", Name(), hour));

    }

    public void TeachHistory(int hour) {

        System.out.println(String.format("%s will give lessons on history for %d hour(s)", Name(), hour));

    }

    public void PlayVideoGame(int hour) {

        System.out.println(String.format("%s has played video game for %d hour(s)", Name(), hour));

    }

    public void WatchMovie(int hour) {

        System.out.println(String.format("%s has played video game for %d hour(s)", Name(), hour));

    }

    public void ListenToMusic(int hour) {

        System.out.println(String.format("%s has played video game for %d hour(s)", Name(), hour));

    }

}

 

//InterfaceDemo.java

public class InterfaceDemo {

    public static void main(String args[]) {

        Teacher wang = new Teacher("Teacher Wang"36);

        Student xiaoming = new Student("Xiaoming"19);

        wang.TeachMath(2);

        xiaoming.StudyMath(1);

        xiaoming.PlayVideoGame(1);

    }

}

 

//Teacher Wang will give lessons on math for 2 hour(s)

//Xiaoming will study math for 1 hour(s)

//Xiaoming has played video game for 1 hour(s)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值