第六次作业

本文通过实例展示了Java中抽象类Base与具体子类Child、Fish和Bird的继承关系,以及如何通过弱引用和多态实现不同类型的动物对象调用信息方法。还介绍了Phone抽象类和其三个子类的电话功能,以及用户界面控制的电话拨打过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题


/*
author:kuokuo
 */
//       父类 Base
    class Base{
        public Base(){
            System.out.println("Base");
        }

    }
//    子类Child继承父类Child
    class Child extends Base{
        public Child(){
            System.out.println("Child");
        }
    }
//测试
    class sample{
        public static void main(String[] args) {
            Child c=new Child();
        }
}

结果

Base
Child

进程已结束,退出代码为 0

第二题


import java.util.WeakHashMap;

/*
author:Kuo
 */
//父类Animal
abstract class Animal { 
    private String color;
    private String type;
    private int age;
    private int weight;
    public String getColor(){
        return color;
    }
    public void setColor(String color){
        this.color=color;
    }
    public String gettype(){
        return type;
    }
    public void setType(String type){
        this.type=type;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age=age;
    }
    public int getWeight(){
        return weight;
    }
    public void setWeight(int Weight){
        this.weight=weight;
    }
    public abstract void info();          //方法
}


import unit7.test2.Animal;
/*
author:Kuo
 */
//子类Fish
public class Fish extends Animal {
    public String getColor(){
        return super.getColor();
    }
    public void setColor(String color){
        super.setColor(color);
    }
    public String getType(){
        return gettype();
    }
    public void setType(String type){
        super.setType(type);
    }
    public int getAge(){
        return getAge();
    }
    public void  setAge(int age){
        super.setAge(age);
    }
    public int getWeight(){
        return getWeight();
    }
    public void setWeight(int age){
        super.setWeight(age);
    }
    public void info(){
        System.out.println("我是一只"+getWeight()+"重的鱼");
        System.out.println("今年"+getAge()+"岁了");
    }

}


import unit7.test2.Animal;
/*
author:Kuo
 */
//子类Bird
public class Bird extends Animal {
    public String getColor(){
        return super.getColor();
    }
    public void setColor(String color){
        super.setColor(color);
    }
    public String getType(){
        return super.gettype();
    }
    public void setType(String type){
        super.setType(type);
    }
    public int getAge(){
        return super.getAge();
    }
    public void setAge(int age){
        super.setAge(age);
    }
    public int getWeight(){
        return super.getWeight();
    }
    public void setWeight(int weight){
        super.setWeight(weight);
    }
    public void info(){
        System.out.println("我是一只"+getColor()+"的"+gettype());
        System.out.println("今年"+getAge()+"岁了");
    }
}

结果

我是一只红色的鸟
今年4岁了
我是一只红色的鱼
今年2岁了

进程已结束,退出代码为 0

第三题

    
    /*
    author:Kuo
     */
    //父类Phone
    abstract class Phones {
        public abstract void call();
    }
    class BaseStation{
        public Phones callPhone(String type){
            if (type.equals("IPhone")){
                return new IPhone();
            }
            else if (type.equals("APhone")){
                return new APhone();
            }
            else {
                return new WPhone();
            }
        }
    }


/*
author:Kuo
实现方法
 */

class IPhone extends Phones {                      //IPhone继承Phone
    public  void call() {
        System.out.println("苹果手机打电话");
    }
}
class APhone extends Phones {                       //APhone继承Phone
      public  void call() {
            System.out.println("安卓手机打电话");
        }
}
class WPhone extends Phones{                        //WPhone继承Phone
      public  void call(){
                System.out.println("Windows Phone 手机打电话"     );
         }
}


/*
author:Kuo
测试打电话
 */

public class  User {
    static Scanner a = new Scanner(System.in);

    public static void main(String[] args) {
        boolean flag=false;
        String b;
        BaseStation c = new BaseStation();
        String d;
        do {
            System.out.println("请问用什么手机打电话?1.苹果手机    2.安卓手机   3.Windous Phone手机");
            b=a.next();
            if ("1".equals(b)){
                d="IPhone";
            }
            else if ("2".equals(b)){
                d="APhone";
            }
            else {
                d="WPhone";
            }
            Phones phones=c.callPhone(d);
            phones.call();
            System.out.println("继续吗?按n退出,按其他任意字符继续");
            b=a.next();
            if ("n".equals(b)){
                flag=true;
            }
        }while (!flag);
        System.out.println("再见");
    }
}

结果

请问用什么手机打电话?1.苹果手机    2.安卓手机   3.Windous Phone手机
1
苹果手机打电话
继续吗?按n退出,按其他任意字符继续
2
请问用什么手机打电话?1.苹果手机    2.安卓手机   3.Windous Phone手机
3
Windows Phone 手机打电话
继续吗?按n退出,按其他任意字符继续
n
再见

进程已结束,退出代码为 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值