java中的父类和子类不可以放在同一个文件里同时编译码?如下,这样一起编译不通过。分开就可以了
importjava.lang.*;publicclassSi{intwidth;intlength;intzhouchang;publicvoidculzhouchan...
import java.lang.*;
public class Si
{
int width;
int length;
int zhouchang;
public void culzhouchang(){
zhouchang = width * length;
System.out.println(zhouchang);
}
}
public class Changfang extends Si
{
int square;
public void zheng(){
square = width * length;
System.out.println(square);
}
public void chang(){
square = width * length;
System.out.println(square);
}
public static void main(String args[]){
Changfang a = new Changfang();
a.width = 10;
a.length = 2;
a.culzhouchang();
a.zheng();
a.chang();
}
} 展开
public class Si
{
int width;
int length;
int zhouchang;
public void culzhouchang(){
zhouchang = width * length;
System.out.println(zhouchang);
}
}
public class Changfang extends Si
{
int square;
public void zheng(){
square = width * length;
System.out.println(square);
}
public void chang(){
square = width * length;
System.out.println(square);
}
public static void main(String args[]){
Changfang a = new Changfang();
a.width = 10;
a.length = 2;
a.culzhouchang();
a.zheng();
a.chang();
}
} 展开
2个回答
展开全部
同一个java文件里只能有一个public类,且该public类与文件名相同。
你这写了2个puiblic类 当然报错了
改成
import java.lang.*;
class Si
{
int width;
int length;
int zhouchang;
public void culzhouchang(){
zhouchang = width * length;
System.out.println(zhouchang);
}
}
public class Changfang extends Si
{
int square;
public void zheng(){
square = width * length;
System.out.println(square);
}
public void chang(){
square = width * length;
System.out.println(square);
}
public static void main(String args[]){
Changfang a = new Changfang();
a.width = 10;
a.length = 2;
a.culzhouchang();
a.zheng();
a.chang();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询