2013-03-18 82 views
-3

我试图运行此代码(在3个不同的类 - A,B和C),它没有运行。我不知道为什么不。任何人都可以帮忙吗?如何让这段代码运行?

public class A {  

    private int number; 
    protected String name; 
    public double price; 

    public A () { 
     System.out.println ("A() called"); } 

    private void foo1() { 
     System.out.println("A version of foo1() called"); } 

    protected int foo2(){ 
     System.out.println("A version of foo2() called"); 
     return number; } 

    public String foo3(){ 
     System.out.println("A version of foo3() called"); 
     return "Hi";    
    } 
} 

public class B extends A { 

    private char service; 

    public B () { 
     super(); 

     System.out.println("B() called"); } 

    public void foo1 () { 
     System.out.println("B version of foo1() called"); } 

    protected int foo2 (){ 
     int n = super.foo2(); 
     System.out.println("B version of foo2() called"); 
     return (n+5); } 

    public String foo3 () { 
     String temp = super.foo3 (); 
     System.out.println("B version of foo3()"); 
     return (temp + " foo3"); } } 

public class C extends B { 

    public C(){ 
     super(); 
     System.out.println("C() called"); } 

    public void foo1 () { 
     System.out.println("C version of foo1() called"); 
    } 
} 
+0

什么,当你尝试运行它发生?什么是你看到的确切的异常或错误? – 2013-03-18 00:37:07

+0

显示你的错误! – Kickaha 2013-03-18 00:38:11

+2

什么,确切地说,你的意思是“它没有运行”?它是否编译?你期望它做什么,它究竟做了什么?程序是否启动,但做错了事,或者你不能启动它? – 2013-03-18 00:38:17

回答

2

你没有main方法来指定程序的开始。你需要在你的一个类来创建一个主要方法有以下特征:

+0

是的我试过了,每次插入它(不管什么类)它都会给我带来错误。 – Slink 2013-03-18 01:47:53

2

类似的东西应该让你的程序运行

public static void main(String[] args) 
{ 
    A a = new A(); 
    a.foo2(); 
    a.foo3(); 

    //same for B and C 
} 
+0

这工作,非常感谢你! – Slink 2013-03-18 01:58:55

+0

这将永远不会编译。由于类A中的方法foo1()是私有方法。 – Jayamohan 2013-03-18 02:06:22

+1

如果有效,请将答案投给! – 2013-03-18 02:24:53

0

再添加一个类在同一个包。例如,我有一个创建一个名为类应用象下面这样:

public class Application{ 
     public static void main(String []args){ 
       A a = new A(); 
       a.foo3(); 
       B b = new B(); 
       b.foo1(); 
       C c = new C(); 
       c.foo1(); 
     } 
    } 

然后,请再次尝试运行它...

+0

这将永远不会编译。由于类A中的方法foo1()是私有方法。 – Jayamohan 2013-03-18 02:04:07

+0

哦..谢谢..我已经更新了它。 – Ericton 2013-03-18 02:09:47