当前位置: 技术问答>java相关
通过一段代码纠正概念错误
来源: 互联网 发布时间:2015-09-23
本文导语: 我刚开始接触JAVA,我发现有很多概念不清,写了一段自以为正确的代码,但是在JBuild6 上并不通过,我应该没有逻辑和语法错误,也许是一些概念不清,代码本身有原则性的错误,希望各位给予指导,谢谢。 abstract...
我刚开始接触JAVA,我发现有很多概念不清,写了一段自以为正确的代码,但是在JBuild6 上并不通过,我应该没有逻辑和语法错误,也许是一些概念不清,代码本身有原则性的错误,希望各位给予指导,谢谢。
abstract class car{ //abstract class about car
//public data field declare
public String color; //car's color
public int gearNum; //car's speed
public String tiretype; //car's tire type
public float engine; //car's engine amount
//public abstract method declare
public abstract void shiftgear(); //change speed
public abstract void brake(); //brake
public abstract void aircon(); //open air condition
public abstract void headlight();//open light
}
//-------------------------------------------------------------------
class jcar extends car{
static int gearNum=5;
static String color="red";
public jcar(){ //constrctor function
tiretype="Dongfeng";
engine=1998.9f;
}
public void paintcar(){//paint car information
System.out.println("car's color is: "+color);
System.out.println("car's speed is: "+gearNum);
System.out.println("car's tire type is: "+tiretype);
System.out.println("car's engine amount is: "+engine);
}
public void shiftgear(){
System.out.println("shiftgear method");
}
public void brake(){
System.out.println("brake");
}
public void aircon(){
System.out.println("air condition");
}
public void headlight(){
System.out.println("head light");
}
}
//-------------------------------------------------------------------
public class mycar extends jcar{
public mycar(){ //set car's color
color="blue";
}
public static void main(){
mycar a1=new mycar();
a1.paintcar();
a1.shiftgear();
a1.brake();
a1.aircon();
a1.headlight();
}
}
abstract class car{ //abstract class about car
//public data field declare
public String color; //car's color
public int gearNum; //car's speed
public String tiretype; //car's tire type
public float engine; //car's engine amount
//public abstract method declare
public abstract void shiftgear(); //change speed
public abstract void brake(); //brake
public abstract void aircon(); //open air condition
public abstract void headlight();//open light
}
//-------------------------------------------------------------------
class jcar extends car{
static int gearNum=5;
static String color="red";
public jcar(){ //constrctor function
tiretype="Dongfeng";
engine=1998.9f;
}
public void paintcar(){//paint car information
System.out.println("car's color is: "+color);
System.out.println("car's speed is: "+gearNum);
System.out.println("car's tire type is: "+tiretype);
System.out.println("car's engine amount is: "+engine);
}
public void shiftgear(){
System.out.println("shiftgear method");
}
public void brake(){
System.out.println("brake");
}
public void aircon(){
System.out.println("air condition");
}
public void headlight(){
System.out.println("head light");
}
}
//-------------------------------------------------------------------
public class mycar extends jcar{
public mycar(){ //set car's color
color="blue";
}
public static void main(){
mycar a1=new mycar();
a1.paintcar();
a1.shiftgear();
a1.brake();
a1.aircon();
a1.headlight();
}
}
|
稍微修改了下,请各位高手指教
abstract class car
{
//abstract class about car
//public data field declare
String color; //car's color
int gearNum; //car's speed
String tiretype; //car's tire type
float engine; //car's engine amount
//public abstract method declare
public abstract void shiftgear(); //change speed
public abstract void brake(); //brake
public abstract void aircon(); //open air condition
public abstract void headlight();//open light
}
//-------------------------------------------------------------------
class jcar extends car
{
static int gearNum=5;
static String color="red";
public void jcar()
{ //constrctor function
tiretype="Dongfeng";
engine=1998.9f;
}
public void paintcar()
{ //paint car information
System.out.println("car's color is: "+color);
System.out.println("car's speed is: "+gearNum);
System.out.println("car's tire type is: "+tiretype);
System.out.println("car's engine amount is: "+engine);
}
public void shiftgear()
{
System.out.println("shiftgear method");
}
public void brake(){
System.out.println("brake");
}
public void aircon(){
System.out.println("air condition");
}
public void headlight(){
System.out.println("head light");
}
}
//-------------------------------------------------------------------
public class mycar
{
public static void main(String args[])
{
jcar a1=new jcar();
a1.paintcar();
a1.shiftgear();
a1.brake();
a1.aircon();
a1.headlight();
}
}
abstract class car
{
//abstract class about car
//public data field declare
String color; //car's color
int gearNum; //car's speed
String tiretype; //car's tire type
float engine; //car's engine amount
//public abstract method declare
public abstract void shiftgear(); //change speed
public abstract void brake(); //brake
public abstract void aircon(); //open air condition
public abstract void headlight();//open light
}
//-------------------------------------------------------------------
class jcar extends car
{
static int gearNum=5;
static String color="red";
public void jcar()
{ //constrctor function
tiretype="Dongfeng";
engine=1998.9f;
}
public void paintcar()
{ //paint car information
System.out.println("car's color is: "+color);
System.out.println("car's speed is: "+gearNum);
System.out.println("car's tire type is: "+tiretype);
System.out.println("car's engine amount is: "+engine);
}
public void shiftgear()
{
System.out.println("shiftgear method");
}
public void brake(){
System.out.println("brake");
}
public void aircon(){
System.out.println("air condition");
}
public void headlight(){
System.out.println("head light");
}
}
//-------------------------------------------------------------------
public class mycar
{
public static void main(String args[])
{
jcar a1=new jcar();
a1.paintcar();
a1.shiftgear();
a1.brake();
a1.aircon();
a1.headlight();
}
}
|
你的主要错误是public static void main(){
这一句。
main方法必须严格按照public static void main(String args[]){
或public static void main(String[] args){
书写。
同时,一个类的数据成员一般定义为private,你的程序里汽车的属性都定义为public,是一种不好的编程风格。
另外,jcar中定义的gearNum和color既改变了访问控制(由public变成了默认访问),又改变了静态性(由非static变成了static),由于这些都是属性缩小的改变,编译还是可以通过的,但也欠妥。
这一句。
main方法必须严格按照public static void main(String args[]){
或public static void main(String[] args){
书写。
同时,一个类的数据成员一般定义为private,你的程序里汽车的属性都定义为public,是一种不好的编程风格。
另外,jcar中定义的gearNum和color既改变了访问控制(由public变成了默认访问),又改变了静态性(由非static变成了static),由于这些都是属性缩小的改变,编译还是可以通过的,但也欠妥。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。