求大神看下 这个JAVA程序怎么不能运行
packagea;publicclassshape{Stringshapetype;publicvoidShape(){System.out.print("Shapeis...
package a;
public class shape
{
String shapetype;
public void Shape()
{
System.out.print("Shape is initialized!");
}
public void draw()
{
System.out.print("shape draw () is called");
}
class Circle extends shape
{
public Circle ()
{
System.out.print("circle is initialized");
}
public void draw ()
{
System.out.print("circle draw () is called");
}
}
class Square extends shape
{
public Square ()
{
System.out.print("square is initialized");
}
public void draw ()
{
System.out.print("square draw () is called");
}
}
class Trine extends shape
{
public Trine ()
{
System.out.print("trine is initialized");
}
public void draw ()
{
System.out.print("trine draw () is called");
}
}
public class PolyTest
{
private void foo(shape shape)
{
shape.draw();
}
}
public void main (String [] args )
{
PolyTest polytest=new PolyTest();
Circle circle=new Circle();
Square square=new Square();
Trine trine=new Trine ();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
} 展开
public class shape
{
String shapetype;
public void Shape()
{
System.out.print("Shape is initialized!");
}
public void draw()
{
System.out.print("shape draw () is called");
}
class Circle extends shape
{
public Circle ()
{
System.out.print("circle is initialized");
}
public void draw ()
{
System.out.print("circle draw () is called");
}
}
class Square extends shape
{
public Square ()
{
System.out.print("square is initialized");
}
public void draw ()
{
System.out.print("square draw () is called");
}
}
class Trine extends shape
{
public Trine ()
{
System.out.print("trine is initialized");
}
public void draw ()
{
System.out.print("trine draw () is called");
}
}
public class PolyTest
{
private void foo(shape shape)
{
shape.draw();
}
}
public void main (String [] args )
{
PolyTest polytest=new PolyTest();
Circle circle=new Circle();
Square square=new Square();
Trine trine=new Trine ();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
} 展开
4个回答
展开全部
1. main 方法 要加static
public static void main (String [] args )
2. 你的类都是定义在内部的,不能直接访问,改成下面的方式
shape sp = new shape();
PolyTest polytest = sp.new PolyTest();
Circle circle = sp.new Circle();
Square square = sp.new Square();
Trine trine = sp.new Trine();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
public static void main (String [] args )
2. 你的类都是定义在内部的,不能直接访问,改成下面的方式
shape sp = new shape();
PolyTest polytest = sp.new PolyTest();
Circle circle = sp.new Circle();
Square square = sp.new Square();
Trine trine = sp.new Trine();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
展开全部
main方法加上static
而且都是内部类,使用时要用主类的对象使用
而且都是内部类,使用时要用主类的对象使用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改成这样:
public class PolyTest {
private void foo(shape shape) {
shape.draw();
}
public static void main(String[] args) {
PolyTest polytest = new PolyTest();
Circle circle = new Circle();
Square square = new Square();
Trine trine = new Trine();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
}
class shape {
String shapetype;
public void Shape() {
System.out.print("Shape is initialized!");
}
public void draw() {
System.out.print("shape draw () is called");
}
}
class Circle extends shape {
public Circle() {
System.out.print("circle is initialized");
}
public void draw() {
System.out.print("circle draw () is called");
}
}
class Square extends shape {
public Square() {
System.out.print("square is initialized");
}
public void draw() {
System.out.print("square draw () is called");
}
}
class Trine extends shape {
public Trine() {
System.out.print("trine is initialized");
}
public void draw() {
System.out.print("trine draw () is called");
}
}
public class PolyTest {
private void foo(shape shape) {
shape.draw();
}
public static void main(String[] args) {
PolyTest polytest = new PolyTest();
Circle circle = new Circle();
Square square = new Square();
Trine trine = new Trine();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
}
class shape {
String shapetype;
public void Shape() {
System.out.print("Shape is initialized!");
}
public void draw() {
System.out.print("shape draw () is called");
}
}
class Circle extends shape {
public Circle() {
System.out.print("circle is initialized");
}
public void draw() {
System.out.print("circle draw () is called");
}
}
class Square extends shape {
public Square() {
System.out.print("square is initialized");
}
public void draw() {
System.out.print("square draw () is called");
}
}
class Trine extends shape {
public Trine() {
System.out.print("trine is initialized");
}
public void draw() {
System.out.print("trine draw () is called");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void main (String [] args )
{
PolyTest polytest=new PolyTest();
Circle circle=new Circle(); ---------改成Square rcle=new Circle();
Square square=new Square();
Trine trine=new Trine (); --------- 改成Square trine=new Trine ();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
}
{
PolyTest polytest=new PolyTest();
Circle circle=new Circle(); ---------改成Square rcle=new Circle();
Square square=new Square();
Trine trine=new Trine (); --------- 改成Square trine=new Trine ();
polytest.foo(circle);
polytest.foo(square);
polytest.foo(trine);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询