抽像方法问题
请问一下,我这个程序怎样改才能正确运行呢,最好能说说理由,谢谢!!import java.lang.Math.*;
abstract class shape{
abstract void area();
}
class rectangle extends shape{
float width,height;
public void retangle(float a,float b){
width=a;height=b;}
public void area(){
System.out.println("Area is "+(width*height));
}
}
class circle extends shape{
float radius;
public void circle(float r){
radius=r;}
public void area(){
System.out.println("Area is "+(radius*radius*Math.PI));
}
}
public class test1{
public static void main(String args[])
{rectangle rect=new rectangle(5,10);
rect.area();
circle cir=new circle(2);
cir.area();
}
}
----------------解决方案--------------------------------------------------------
import java.lang.Math.*;
abstract class shape{
abstract void area();
}
class rectangle extends shape{
float width,height;
public rectangle(int i, int j) {
// TODO Auto-generated constructor stub
}
public void retangle(float a,float b){
width=a;height=b;}
public void area(){
System.out.println("Area is "+(width*height));
}
}
class circle extends shape{
float radius;
public circle(int i) {
// 这类没定义!
}
public void circle(float r){
radius=r;}
public void area(){
System.out.println("Area is "+(radius*radius*Math.PI));
}
}
public class Fushu{
public static void main(String args[])
{rectangle rect=new rectangle(5,10);
rect.area();
circle cir=new circle(2);
cir.area();
}
}
----------------解决方案--------------------------------------------------------
//import java.lang.Math.*;这引用没什么意义啊!
abstract class shape{
abstract void area();
}
class rectangle extends shape{
float width,height;
public rectangle(int i, int j) {
// 这类的构造方法怎么没写?
}
public void retangle(float a,float b){
width=a;height=b;}
public void area(){
System.out.println("Area is "+(width*height));
}
}
class circle extends shape{
float radius;
public circle(int i) {
// 这类没定义!
}
public void circle(float r){
radius=r;}
public void area(){
System.out.println("Area is "+(radius*radius*Math.PI));
}
}
public class Fushu{
public static void main(String args[])
{rectangle rect=new rectangle(5,10);
rect.area();
circle cir=new circle(2);
cir.area();
}
}
----------------解决方案--------------------------------------------------------
路过
LZ再仔细看看
改的不怎么好
在上课,呵呵
----------------解决方案--------------------------------------------------------
[CODE]abstract class shape{
abstract void area();
}
class rectangle extends shape{
float width,height;
public rectangle(float a,float b){
width=a;height=b;}
public void area(){
System.out.println("Area is "+(width*height));
}
}
class circle extends shape{
float radius;
public circle(float r){
radius=r;}
public void area(){
System.out.println("Area is "+(radius*radius*Math.PI));
}
}
public class test1{
public static void main(String args[]) {
rectangle rect=new rectangle(5,10);
rect.area();
circle cir=new circle(2);
cir.area();
}
}[/CODE]
这是能运行的程序
----------------解决方案--------------------------------------------------------
import java.lang.Math.*;//这是什么,静态导入吗?错
abstract class shape{
abstract void area();
}
class rectangle extends shape{
float width,height;
public void retangle(float a,float b){//构造函数不能有返回值,还有你的rectangle打错了,少了个c
width=a;height=b;}
public void area(){
System.out.println("Area is "+(width*height));
}
}
class circle extends shape{
float radius;
public void circle(float r){//同理,这个也不是构造函数,把void去掉才是构造函数
radius=r;}
public void area(){
System.out.println("Area is "+(radius*radius*Math.PI));
}
}
public class test1{
public static void main(String args[])
{rectangle rect=new rectangle(5,10);
rect.area();
circle cir=new circle(2);
cir.area();
}
}
----------------解决方案--------------------------------------------------------
朋友,听听千里老大的吧
改的满好!
----------------解决方案--------------------------------------------------------
呵呵,谢谢你们这班有心人了!
----------------解决方案--------------------------------------------------------
不用谢,大家互相帮助嘛
----------------解决方案--------------------------------------------------------