Java 基础篇:第八章:接口练习

 

目录

一、作业题:

1、练习1.【构造方法与重载】

2、练习2.【覆盖、继承】

3、练习3.【抽象类】

二、回顾:

2.1 继承:


主要内容:

1、接口

 

一、作业题:

1、练习1.【构造方法与重载】

为“无名的粉”写一个类class WuMingFen,要求:1.有三个属性,面码:String theMa、粉的分量(两)int quantity、是否带汤boolean likeSoup ;2.写一个构造方法,以便于简化初始化过程,如WuMingFen f1 = new WuMingFen("牛肉",3,true); 3.重载构造方法使得初始化过程可以多样化WuMingFen f2 = new WuMingFen("牛肉",2); 4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的WuMingFen f3 = new WuMingFen(); 5.写一个普通方法check(),用于查看粉是否符合要求。即将对象的三个属性打印在控制台上。

public class WuMingFen {

    String theMa;

    int quantity;

    boolean lookSoup;

 

    public WuMingFen() { // 无参构造器

    }

 

    public WuMingFen(String theMa, int quantity, boolean lookSoup) { // 有参构造器

       this.theMa = theMa;

       this.quantity = quantity;

       this.lookSoup = lookSoup;

    }

 

    public WuMingFen(String theMa, int quantity) { // 有参构造器

       this.theMa = theMa;

       this.quantity = quantity;

    }

 

    public void check() {

       System.out.println("theMa:" + theMa + ",quantity:" + quantity

              + ",lookSoup:" + lookSoup);

    }

}

 

public class MyTest {

    public static void main(String[] args) {

       // 第一个要求

       WuMingFen wumingfen1 = new WuMingFen("牛肉", 3, true);

       wumingfen1.check();

       // 第二个要求

       WuMingFen wumingfen2 = new WuMingFen("牛肉", 2);

       wumingfen2.check();

       // 第三个要求

       WuMingFen wumingfen3 = new WuMingFen();

       wumingfen3.theMa = "酸辣面";

       wumingfen3.quantity = 2;

       wumingfen3.lookSoup = true;

       wumingfen3.check();

    }

}

代码格式化:

1、默认是Ctrl+Shift+F     无效(原因:快捷键冲突),修改快捷键

Preferences->General->Keys   Format

2、Preferences->Java->Editors->Save Actions

 

 

2、练习2.【覆盖、继承】

建立一个汽车Auto类,包括轮胎个数、汽车颜色、车身重量、速度等成员变量、并通过不同的构造方法创建实例。至少要求汽车能够加速、减速、停车。再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量和覆盖加速、减速的方法。

public class Auto {

    int number;// 轮胎个数

    String color;

    double weight;

    double speed;

 

    public Auto() {

 

    }

 

    public Auto(int number, String color, double weight, double speed) {

       this.number = number;

       this.color = color;

       this.weight = weight;

       this.speed = speed;

    }

 

    public void speedUp() {

       System.out.println("车子加速!!!!");

    }

 

    public void speedDown() {

       System.out.println("车子减速!!!!");

    }

 

    public void stop() {

       System.out.println("车子停止!!!!");

    }

}

 

public class Car extends Auto {

    String airCondition;

    String cd;

 

    public Car() {

 

    }

 

    public Car(String airCondition, String cd, int number, String color,

           double weight, double speed) {

       super(number, color, weight, speed); // 父类有参构造器

       this.airCondition = airCondition;

       this.cd = cd;

    }

 

    public void speedUp() {

       System.out.println("小汽车加速!!!!");

    }

 

    public void speedDown() {

       System.out.println("小汽车减速!!!!");

    }

}

public class MyTest {

    public static void main(String[] args) {

       Car car = new Car("海尔", "VCD/DVD/DVCD", 4, "red", 2000.00, 0.0);

       car.speedDown();

       car.speedUp();

       car.stop();

    }

}

 

3、练习3.【抽象类】

创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明一个NoOfWheels方法,使它返回一个字符串值。创建两个类Car和Motorbike从Vehicle类继承,并在这两个类中实现NoOfWheels方法。在Car类中,应当显示“四轮车”信息;而在Motorbike类中应当显示“双轮车”信息。创建另一个带main方法的类,在该类中创建Car和Motorbike的实例,并在控制台中显示消息。

public abstract class Vehicle {

         public abstract void NoOfWheels();

}

public class Car extends Vehicle {

         public void NoOfWheels() {

                   System.out.println("四轮车!!!!");

         }

}

public class Motorbike extends Vehicle {

       public void NoOfWheels() {

                 System.out.println("双轮车!!!!");

       }

}

public class Main {

         public static void main(String[] args) {

                   Vehicle Car=new Car();  //多态,向上转型

                   Car.NoOfWheels();

                   Vehicle Motorbike=new Motorbike();

                   Motorbike.NoOfWheels();

         }

}

二、回顾:

2.1 继承:

1、继承的特点:

只支持单继承

支持多层继承

超类拥有公共非私有方法,最底层的子类拥有全部非私有方法

2、继承的成员访问:

成员变量:不同名的非私有成员变量可以直接方法,同名的非私有成员变量遵循就近原则。

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值