关于泛型的问题,我是新手,不大理解及使用,请大虾们不吝赐教!!
程序代码:
import java.util.*;
interface I1{
float getArea();
}
class Triangle implements I1
{
float a;
float b;
float c;
String s = "Triangle";
Triangle (float a,float b,float c){
this.a = a;
this.b = b;
this.c = c;
}
float getArea(){
float p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c ));
}
}
class Rectangle implements I1
{
float width;
float length;
String s = "Rectangle";
float getArea(){
return width * length;
}
};
class Square implements I1
{
float a;
String s = "Square";
float getArea(){
return a * a;
}
};
class Circle implements I1
{
float r;
String s = "Circle";
final float PI = 3.1415926f;
float getArea(){
return PI*r*r;
}
};
public void printArea(T){
System.out.println("The area of this" + T.s + ":" + T.getArea() );
}
public class GraphicsTest
{
public static void main(String[] args){
List<int>strList = new ArrayList<int>();
strList.add(t.getArea());
strList.add(r.getArea());
strList.add(s.getArea());
strList.add(c.getArea());
Triangle t = new Triangle (3.3f,6.6f,5.5f);
Rectangle r = new Retangle(5.6f,3.8f);
printArea(r);
Square s = new Square(2.9f);
printArea(s);
Circle c = new Circle(4.8f);
printArea(c);
}
};
输出的错误结果:interface I1{
float getArea();
}
class Triangle implements I1
{
float a;
float b;
float c;
String s = "Triangle";
Triangle (float a,float b,float c){
this.a = a;
this.b = b;
this.c = c;
}
float getArea(){
float p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c ));
}
}
class Rectangle implements I1
{
float width;
float length;
String s = "Rectangle";
float getArea(){
return width * length;
}
};
class Square implements I1
{
float a;
String s = "Square";
float getArea(){
return a * a;
}
};
class Circle implements I1
{
float r;
String s = "Circle";
final float PI = 3.1415926f;
float getArea(){
return PI*r*r;
}
};
public void printArea(T){
System.out.println("The area of this" + T.s + ":" + T.getArea() );
}
public class GraphicsTest
{
public static void main(String[] args){
List<int>strList = new ArrayList<int>();
strList.add(t.getArea());
strList.add(r.getArea());
strList.add(s.getArea());
strList.add(c.getArea());
Triangle t = new Triangle (3.3f,6.6f,5.5f);
Rectangle r = new Retangle(5.6f,3.8f);
printArea(r);
Square s = new Square(2.9f);
printArea(s);
Circle c = new Circle(4.8f);
printArea(c);
}
};
E:\09008129>javac GraphicsTest.java
GraphicsTest.java:57: 需要为 class、interface 或 enum
public void printArea(T){
^
GraphicsTest.java:62: 需要为 class、interface 或 enum
}
^
2 错误
//这是什么意思啊!我这道题要求用泛型输出各种图形的面积,可是我似乎不大理解泛型,估计是泛型的那个方法写错了,应该怎么修改呢?为什么?还希望各位大虾帮忙解答!!小弟在这里谢过了!!
----------------解决方案--------------------------------------------------------
汗,你写的太业余了,不是一句两句能说清楚的~~~
----------------解决方案--------------------------------------------------------
你想要第五十七行的 printArea(T)做什么?
方法是要定义在类里面的
另外,代码还是要注意一下缩进和空行……
想给你排一下程序的,看了下代码,才明白二楼所说的含义!!!
建议楼主再回去好好看书,前面的先学扎实了,那些类,方法,成员变量等的定义和使用先学好了,再看这面向对象的知识……
估计二楼在看的时候,不是在找错的地方,而是在找对的地方……
程序代码:
import java.util.*;
interface I1{
float getArea();
}
class Triangle implements I1
{
float a;
float b;
float c;
String s = "Triangle";
Triangle (float a,float b,float c){
this.a = a;
this.b = b;
this.c = c;
}
float getArea(){
float p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c ));
}
}
class Rectangle implements I1
{
float width;
float length;
String s = "Rectangle";
float getArea(){
return width * length;
}
}; //这个分号是做什么?
class Square implements I1
{
float a;
String s = "Square";
float getArea(){
return a * a;
}
};
class Circle implements I1
{
float r;
String s = "Circle";
final float PI = 3.1415926f;
float getArea(){
return PI*r*r;
}
};
public void printArea(T){
System.out.println("The area of this" + T.s + ":" + T.getArea() );
}
public class GraphicsTest
{
public static void main(String[] args){
List<int>strList = new ArrayList<int>();
strList.add(t.getArea());
strList.add(r.getArea());
strList.add(s.getArea());
strList.add(c.getArea());
Triangle t = new Triangle (3.3f,6.6f,5.5f);
Rectangle r = new Retangle(5.6f,3.8f);
printArea(r);
Square s = new Square(2.9f);
printArea(s);
Circle c = new Circle(4.8f);
printArea(c);
}
};
interface I1{
float getArea();
}
class Triangle implements I1
{
float a;
float b;
float c;
String s = "Triangle";
Triangle (float a,float b,float c){
this.a = a;
this.b = b;
this.c = c;
}
float getArea(){
float p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c ));
}
}
class Rectangle implements I1
{
float width;
float length;
String s = "Rectangle";
float getArea(){
return width * length;
}
}; //这个分号是做什么?
class Square implements I1
{
float a;
String s = "Square";
float getArea(){
return a * a;
}
};
class Circle implements I1
{
float r;
String s = "Circle";
final float PI = 3.1415926f;
float getArea(){
return PI*r*r;
}
};
public void printArea(T){
System.out.println("The area of this" + T.s + ":" + T.getArea() );
}
public class GraphicsTest
{
public static void main(String[] args){
List<int>strList = new ArrayList<int>();
strList.add(t.getArea());
strList.add(r.getArea());
strList.add(s.getArea());
strList.add(c.getArea());
Triangle t = new Triangle (3.3f,6.6f,5.5f);
Rectangle r = new Retangle(5.6f,3.8f);
printArea(r);
Square s = new Square(2.9f);
printArea(s);
Circle c = new Circle(4.8f);
printArea(c);
}
};
[ 本帖最后由 洛云 于 2011-4-4 01:10 编辑 ]
----------------解决方案--------------------------------------------------------