老师我在elipse里写完后copy过来就这样了
来源:2-22 编程练习
Gudalow7
2020-02-21 13:48:03
运行失败
Tricycle.java:8: error: class test is public, should be declared in a file named test.java
public class test {
^
Tricycle.java:6: error: cannot find symbol
import com.imooc.whatbike.Tricycle;
^
symbol: class Tricycle
location: package com.imooc.whatbike
Tricycle.java:20: error: cannot find symbol
Tricycle tc1 = new Tricycle();
^
symbol: class Tricycle
location: class test
Tricycle.java:20: error: cannot find symbol
Tricycle tc1 = new Tricycle();
^
symbol: class Tricycle
location: class test
4 errors
=============================================
package test;
import com.imooc.whatbike.Bicycle;
import com.imooc.whatbike.ElectricVehicle;
import com.imooc.whatbike.NonMotor;
import com.imooc.whatbike.Tricycle;
public class test {
public static void main(String[] args) {
System.out.print("父类信息测试:");
NonMotor nm1 = new NonMotor("天宇","红");
System.out.println(nm1.work());
System.out.print("自行车类信息测试:");
Bicycle bc1 = new Bicycle();
System.out.println(bc1.work());
System.out.print("电动车类信息测试:");
ElectricVehicle ev1 = new ElectricVehicle("飞鸽");
System.out.println(ev1.work());
System.out.print("三轮车类信息测试:");
Tricycle tc1 = new Tricycle();
System.out.println(tc1.work());
}
}
==========================================
package com.imooc.whatbike;
/*
* 父类
* */
public class NonMotor {
// 私有属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
private String make;
private String color;
private int wheel = 2;
private int chair = 1;
// 无参构造方法
public NonMotor() {
}
// 双参构造方法,完成对品牌和颜色的赋值
public NonMotor(String make, String color) {
this.setMake(make);
this.setColor(color);
}
// 四参构造方法,分别对所有属性赋值
public NonMotor(String make, String color, int wheel, int chair) {
this.setMake(make);
this.setColor(color);
this.setWheel(wheel);
this.setChair(chair);
}
// 公有的get***/set***方法完成属性封装
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheel() {
return wheel;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public int getChair() {
return chair;
}
public void setChair(int chair) {
this.chair = chair;
}
// 方法:运行,描述内容为:这是一辆**颜色的,**牌的非机动车,有**个轮子,有**个座椅的非机动车。其中**的数据由属性提供
public String work() {
String str = "父类测试信息:这是一辆" + this.getColor() + "颜色的," + this.getMake() + "牌的非机动车,有" + this.getWheel() + "个轮子,有"
+ this.getChair() + "个座椅";
return str;
}
}
============================================================
package com.imooc.whatbike;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
public class Bicycle extends NonMotor {
// 在构造方法中调用父类多参构造,完成属性赋值
public Bicycle () {
super("捷安特", "黄");
}
// 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
public String work() {
String str = "自行车类信息测试:这是一辆" + this.getColor() + "颜色的," + this.getMake() + "牌的自行车。";
return str;
}
}
========================================================
package com.imooc.whatbike;
public class ElectricVehicle extends NonMotor {
// 私有属性:电池品牌
private String batterymake;
//无参构造
public ElectricVehicle() {
}
//带参构造
public ElectricVehicle(String batterymake) {
this.setBatterymake(batterymake);
}
// 公有的get***/set***方法完成属性封装
public String getBatterymake() {
return batterymake;
}
public void setBatterymake(String batterymake) {
this.batterymake = batterymake;
}
// 重写运行方法,描述内容为:这是一辆使用**牌电池的电动车。其中**的数据由属性提供
public String work() {
String str="电动车类信息测试:这是一辆使用"+this.getBatterymake()+"牌电池的电动车。";
return str;
}
}
======================================================================
package com.imooc.whatbike;
public class Tricycle extends NonMotor {
// 在无参构造中实现对轮子属性值进行修改
public Tricycle(){
this.setWheel(3);
}
// 重写运行方法,描述内容为:三轮车是一款有**个轮子的非机动车。其中**的数据由属性提供
public String work() {
String str="三轮车类信息测试:三轮车是一款有"+this.getWheel()+"个轮子的非机动车。";
return str;
}
}
==============================================================
老师我在elipse里面没报错呀 老师我代码写的对吗?
1回答
好帮手慕雪
2020-02-21
请注意
这里是Test,不是test哦。包括不要引用package包。在线编程这些类是在同一目录下的。其它的没有问题哦。如果解决了你的疑惑,请采纳,祝学习愉快~
相似问题
回答 1
回答 1