实验五 类的继承及接口

程序片段编程题

1. 定义接口CircleInterface,类Circle实现接口,实现计算圆的周长和面积,测试其特性。

【问题描述】
定义接口CircleInterface,类Circle实现接口,实现计算圆的周长和面积,测试其特性。
【输入形式】
请输入圆的半径
【输出形式】
圆的面积是:
圆的周长是:
【样例输入】
Please input r: 2
【样例输出】
The area is : 12.56636
The circumference is : 12.56636
【样例说明】
根据样例输出,完成程序填空。

//计算圆的周长和面积
import java.util.Scanner;
interface CircleInterface
{
      final  float  PI=3.14159f;
      float  getArea(float r);
      float  getCircumference(float r);
}
class  Circle  implements  CircleInterface
{
	public float getArea(float r)
	{
		return PI*r*r;
	}
	public float getCircumference(float r) 
	{
		return 2*PI*r;
	}
}

public class Test {
      public static void main(String[] args) {
           Circle c = new Circle();
           Scanner s = new Scanner(System.in);
           System.out.println("Please input r: ");
           float r = s.nextFloat();
           System.out.println("The area is : "+ c.getArea(r));
           System.out.println("The circumference is : "+c.getCircumference(r));
      }
}

2. 定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。

【问题描述】
定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。
【输入形式】
请输入公交车线次:347
请输入出租车名字:yellow
请输入你的车品牌:BMW
【输出形式】
公交车347有6个轮子
公交车347票价2元每人
出租车yellow有4个轮子
出租车yellow为2元每公里
如果这辆BMW车是你的就免费乘坐
【样例输入】
347
Yellow
BMW
【样例输出】
The bus 347 has 6 wheels.
The bus 347 2 yuan per people
The taxi Yellow has 4 wheels.
The taxi Yellow 2 yuan per km
The car BMW is free if it is yours
【样例说明】
根据样例输出,完成程序填空。

import java.util.Scanner;

abstract class Vehicles 
{
    abstract void wheel(String name);
}

interface MoneyFare 
{
    void charge(String name);
}

class Bus extends Vehicles implements MoneyFare 
{
    void wheel(String name) 
    {
    	System.out.println("The bus "+ name +" has 6 wheels.");
    }
    public void charge(String name)  
    {
    	System.out.println("The bus "+ name +" 2 yuan per people");      
    }
}

class Taxi extends Vehicles implements MoneyFare
{
    void wheel(String name)  
    {
    	System.out.println("The taxi "+ name +" has 4 wheels.");
    }
    public void charge(String name)  
    {
    	System.out.println("The taxi "+ name +" 2 yuan per km");       
    }
}

class Car implements MoneyFare
{
     public void charge(String name)  
     {
         System.out.println("The car "+ name +" is free if it is yours");   
     }
}

public class Test  
{
      public static void main(String args[]) 
      {
            Bus bus = new Bus();
            Taxi taxi = new Taxi();
            Car car = new Car();
                
            String busname,taxiname,carname;
            Scanner s = new Scanner(System.in);
                
            System.out.println("Please input the bus line: ");
            busname = s.nextLine();
            System.out.println("Please input the taxi name: ");
            taxiname = s.nextLine();
            System.out.println("Please input the car brand: ");
            carname = s.nextLine();
            bus.wheel(busname);
            bus.charge(busname);
            taxi.wheel(taxiname);
            taxi.charge(taxiname);
            car.charge(carname);
        }
}
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java继承接口、多态是面向对象编程的三大核心特性,以下是它们的实验总结: 1. 继承继承是一种重用代码的方式,它允许我们定义一个新的,该继承了现有的属性和方法。在Java中,继承是通过关键字"extends"来实现的。 在实验过程中,我们可以通过定义一个父,然后在子中使用"extends"关键字继承的属性和方法。例如: ```java class Animal { public void eat() { System.out.println("Animal is eating"); } } class Dog extends Animal { public void bark() { System.out.println("Dog is barking"); } } ``` 在上面的例子中,Dog继承了Animal的eat()方法。我们可以实例化Dog并调用eat()方法,因为Dog继承了Animal的行为。 2. 接口接口是一种定义行为的方式,它定义应该如何相互协作。在Java中,接口是通过关键字"interface"来定义的。 在实验过程中,我们可以通过定义一个接口定义的行为,然后让实现接口。例如: ```java interface Animal { public void eat(); } class Dog implements Animal { public void eat() { System.out.println("Dog is eating"); } } ``` 在上面的例子中,Animal接口定义了eat()方法,然后Dog实现了该接口实现了eat()方法。 3. 多态:多态是一种对象的行为表现方式,它允许我们使用一个父的引用来引用一个子的对象。在Java中,多态是通过继承接口实现的。 在实验过程中,我们可以定义一个父的引用,并使用该引用来引用一个子的对象。例如: ```java class Animal { public void eat() { System.out.println("Animal is eating"); } } class Dog extends Animal { public void eat() { System.out.println("Dog is eating"); } } public class Main { public static void main(String[] args) { Animal a = new Animal(); Animal b = new Dog(); a.eat(); // 输出 "Animal is eating" b.eat(); // 输出 "Dog is eating" } } ``` 在上面的例子中,我们定义了一个Animal和一个Dog,然后在main()方法中定义了一个Animal型的引用a和一个Animal型的引用b,并使用它们来引用一个Animal对象和一个Dog对象。由于Dog继承了Animal的eat()方法,并且重写了该方法,因此在调用b.eat()方法时,输出的是"Dog is eating"。这就是多态的表现方式。 总的来说,继承接口、多态是Java面向对象编程的三大核心特性,它们可以帮助我们更好地组织代码、提高代码的可重用性和可扩展性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值