Java教程

java学习第21天

本文主要是介绍java学习第21天,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

类与接口关系示例:

          package jicheng.com;



public interface Inter1 {

}

package jicheng.com;

public interface Inter2 {
}

package jicheng.com;

public interface Inter3 extends Inter1,Inter2 {
}

package jicheng.com;

public class Interimpl extends Object implements Inter3,Inter2,Inter1 {
}

抽象类与接口的区别:

成员区别:(抽象类)变量、常量,有构造方法,有抽象方法也有非抽象方法。

(接口)常量;抽象方法。

关系区别:类与类是继承且是单继承,类与接口是实现,可以是单实现,也可以多实现。接口与接口是继承可以单继承也可以多继承。

设计理念区别:(抽象类)对类抽象,包括属性和行为,(接口)对行为抽象,主要是行为。

举例:手机在发明之前只能打电话、发短信,发展后能够上网。

定义一个手机抽象类,有打电话发短信功能,定义一个上网的接口。

package jicheng.com;

public class Phone {
    public Phone() {
    }
    public void call(){
        System.out.println("打电话");
    }
    public void message(){
        System.out.println("发短信");
    }
}

package jicheng.com;

public interface Surf
{
    public abstract void surf();
}

package jicheng.com;

public class NewPhone extends Phone implements Surf {
    @Override
    public void surf() {
        System.out.println("surf the internet");
    }

    public NewPhone() {
    }

    public static void main(String[] args) {
        NewPhone n= new NewPhone();
        n.surf();
        n.call();
        n.message();
    }
}

案例:我们现有乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。为了出国交流,跟乒乓球相关的人员都需要学习英语。请用所学知识分析,这个案例需要哪些类哪些接口,并用代码实现。

分析:由具体到抽象。从具体篮球运动员类,乒乓球运动员类抽象出运动员类。从篮球教练类和乒乓球教练类抽象出教练类,从教练类和运动员类抽象出人类,定义一个学习英语接口,最后需要一个测试类。222

package anli;

public interface SpeakE {
    public void speakEnglish();
}

 

package anli;

public abstract class People {
    private int age;
    private String name;

    public People() {
    }

    public People(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract void eat();
    public abstract void work();
}

package anli;

public abstract class Coach extends People{
    public Coach() {
    }

    public Coach(int age, String name) {
        super(age, name);
    }
    public abstract void teach();
}

package anli;

public abstract class Player extends People {
    public Player() {
    }

    public Player(int age, String name) {
        super(age, name);
    }


    public abstract void study();
}

package anli;

public class PingpangCoach extends Coach implements SpeakE {
    public PingpangCoach() {
    }

    public PingpangCoach(int age, String name) {
        super(age, name);
    }

    @Override
    public void eat() {
        System.out.println("吃白菜,喝小米粥");
    }

    @Override
    public void work() {
        System.out.println("乒乓球教练一天工作3小时");
    }

    @Override
    public void teach() {
        System.out.println("乒乓球教练教发球和接球");
    }

    @Override
    public void speakEnglish() {
        System.out.println("乒乓球教练讲英语");
    }

}

package anli;

public class BasketballCoach extends Coach {
    public BasketballCoach() {
    }

    public BasketballCoach(int age, String name) {
        super(age, name);
    }

    @Override
    public void eat() {
        System.out.println("吃牛肉,喝牛奶");
    }

    @Override
    public void work() {
        System.out.println("篮球教练一天工作6小时");
    }

    @Override
    public void teach() {
        System.out.println("篮球教练教运球和扣球");
    }
}

package anli;



public class BasketballPlayer extends Player {

    public BasketballPlayer() {

    }



    public BasketballPlayer(int age, String name) {

        super(age, name);

    }



    @Override

    public void eat() {

        System.out.println("吃牛肉,喝牛奶");

    }



    @Override

    public void work() {

        System.out.println("篮球运动员一天训练8个小时");

    }



    @Override

    public void study() {

        System.out.println("篮球运动员学习扣篮和运球");

    }

}package anli;



public class PingpangPlayer extends Player implements SpeakE {

    public PingpangPlayer() {

    }



    public PingpangPlayer(int age, String name) {

        super(age, name);

    }



    @Override

    public void eat() {

        System.out.println("乒乓球运动员吃蔬菜,喝米粥");

    }



    @Override

    public void work() {

        System.out.println("乒乓球运动员一天训练4小时");

    }



    @Override

    public void study() {

        System.out.println("乒乓球运动员学习发球和接球");

    }



    @Override

    public void speakEnglish() {

        System.out.println("乒乓球运动员讲英语");

    }

}

package anli;

public class Demo {
    public static void main(String[] args) {
        System.out.println("教练员示例:");
        PingpangCoach c1=new PingpangCoach(48,"刘国梁");
        c1.eat();
        c1.work();
        c1.teach();
        c1.speakEnglish();
        System.out.println(c1.getName()+","+c1.getAge());
        System.out.println("---------------");
        System.out.println("运动员示例:");
        BasketballPlayer b1=new BasketballPlayer(28,"姚明");
        b1.eat();
        b1.work();
        b1.study();
        System.out.println(b1.getName()+","+b1.getAge());
    }

}

D:\Java\jdk-16.0.1\bin\java.exe -Didea.launcher.port=63736 "-Didea.launcher.bin.path=D:\IntelliJ IDEA 2018.2.8\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\dell\IdeaProjects\java学习\out\production\java学习;D:\IntelliJ IDEA 2018.2.8\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 anli.Demo

教练员示例:

吃白菜,喝小米粥

乒乓球教练一天工作3小时

乒乓球教练教发球和接球

乒乓球教练讲英语

刘国梁,48

---------------

运动员示例:

吃牛肉,喝牛奶

篮球运动员一天训练8个小时

篮球运动员学习扣篮和运球

姚明,28

 

Process finished with exit code 0

类名作为形参和返回值

方法的形参是类名,其实需要的是该类的对象

方法的返回值是类名,其实返回的是该类的对象

示例:

package anli;

public class Cat {
    public void eat(){
        System.out.println("猫吃鱼");
    }
}

package anli;

public class CatOperator {
    public void useCat(Cat c){
        c.eat();
    }
    public Cat getCat(){
        Cat c=new Cat();
        return c;
    }
}

package anli;

public class CatDemo {
    public static void main(String[] args) {
        CatOperator c=new CatOperator();
        Cat c1=new Cat();
        c.useCat(c1);
        Cat c2=c.getCat();
        c2.eat();
    }
}

D:\Java\jdk-16.0.1\bin\java.exe -Didea.launcher.port=64606 "-Didea.launcher.bin.path=D:\IntelliJ IDEA 2018.2.8\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\dell\IdeaProjects\java学习\out\production\java学习;D:\IntelliJ IDEA 2018.2.8\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 anli.CatDemo

猫吃鱼

猫吃鱼

 

Process finished with exit code 0

抽象类名作为形参和返回值

方法形参是抽象类名,其实需要的是该抽象类的子类对象

方法的返回值是抽象类名,其实返回值是该抽象类的子类对象

示例

package anli;

public abstract class Animal {
    public abstract void eat();
}

package anli;

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
}

 

package anli;

public class AnimalOperator {
    public void useAnimal(Animal a){
        a.eat();
    }
    public Animal getAnimal() {
        Animal a = new Dog();
        return a;
    }
}

package anli;

public class AnimalDemo1 {
    public static void main(String[] args) {
        AnimalOperator a=new AnimalOperator();
        Animal a1=new Dog();
        a.useAnimal(a1);
        Animal a2=a.getAnimal();
        a2.eat();
    }
}

D:\Java\jdk-16.0.1\bin\java.exe -Didea.launcher.port=57645 "-Didea.launcher.bin.path=D:\IntelliJ IDEA 2018.2.8\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\dell\IdeaProjects\java学习\out\production\java学习;D:\IntelliJ IDEA 2018.2.8\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 anli.AnimalDemo1

狗吃骨头

狗吃骨头

 

Process finished with exit code 0

 

 

 

这篇关于java学习第21天的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!