1 | 【问题描述】 圆的面积是: 圆的周长是: |
package com;
import java.util.Scanner;
interface CircleInterface
{
final float PI=3.14159f;
float getArea(float r);
float getCircumference(float r);
}
class Circle implements CircleInterface
{
float r ;
public void Circle(float r){
this.r=r;
}
public float getArea(float r){
return (float) (PI*Math.pow(r,2));
}
public float getCircumference(float r){
return PI*2*r;
}
}
public class P4T2 {
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));
}
}
注意:
Illegal modifier for the interface field CircleInterface.r; only public, static & final are permitted
接口 里面不能用private
The type Circle must implement the inherited abstract method CircleInterface.area(float)
抽象类才能继承接口
Cannot instantiate the type Circle 无法实例化类型
——因为Circle 作为抽象类,不能实例化
Syntax error, insert ";" to complete Statement ——中间需要插入 “ + ”
Syntax error on token "float", delete this token
Exception in thread "main" java.lang.NoSuchMethodError: com.Circle.area(F)F
at com.jiekou.main(jiekou.java:36)
——定义的类重复了
2 | 【问题描述】 Car实现接口,测试其特性。 请输入出租车名字:yellow 请输入你的车品牌:BMW
公交车347有6个轮子 公交车347票价2元每人 出租车yellow有4个轮子 出租车yellow为2元每公里 如果这辆BMW车是你的就免费乘坐 |
package com;
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 P4T4 {
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);
}
}
注意:
The public type Bus must be defined in its own file
Syntax error on token "class", @ expected
——删除抽象类后面的括号
Syntax error on token "class", @ expected
——string 中的 s 应该大写
类可以重新new出实例
Abstract methods do not specify a body
——抽象方法不需要定义变量
The method wheel(String) in the type Bus is not applicable for the arguments (double)
——前面用了string,后面就不能用double了
抽象类或者接口里面的方法并不需要{ }这个了
最后直接调用就行 类.方法.(变量)