2013年9月10日星期二

Post a novice compilation issues

inner classes
I have compiled an internal class example
http://lavasoft.blog.51cto.com/62575/179484/


/** 
* 内部类创建与初始化 

* @author leizhimin 2009-7-17 13:51:52 
*/ 
public class Outer { 
        private int i = 10; 
        private int y = 8; 

        Outer() { 
                System.out.println("调用Outer构造方法:outer"); 
        } 

        public void sayMsg() { 
                System.out.println("Outer class!"); 
        } 

        class Inner { 
                int i = 1000; 

                Inner() { 
                        System.out.println("调用Inner构造方法:inner"); 
                } 

                void innerMsg() { 
                        System.out.println(">>>>>Inner class!"); 
                        sayMsg(); 
                        //访问内部类自己的成员i,也可以写成 this.i++ 
                        this.i++; 
                        //访问外部类的成员 i和y 
                        Outer.this.i++; 
                        y--; 
                } 

                int getI() { 
                        return i; 
                } 
        } 

        public void test() { 
                Inner in = new Inner(); 
                in.innerMsg(); 
        } 

        public int getI() { 
                return i; 
        } 

        public void setI(int i) { 
                this.i = i; 
        } 


class Test1 { 
        public static void main(String[] args) { 
                Outer outer = new Outer(); 
                outer.test(); 
                System.out.println(outer.getI()); 
                System.out.println("-------1--------"); 

                Outer.Inner iner = outer.new Inner(); 
                iner.innerMsg(); 
                System.out.println(iner.getI()); 
                System.out.println("-------2--------"); 

                System.out.println(outer.getI()); 
        } 
}

javac Test1.java prompts the following error
Test1.java: 6: class Outer is public, should be declared in a file named Outer.java
public class Outer {
^
1 error

=============
compile this example of inner classes also the case when
http://blog.csdn.net/ilibaba/article/details/3866537?reload

public interface Contents {
int value();
}
public interface Destination {
String readLabel();
}
public class Goods {
private class Content implements Contents {
private int i = 11;
public int value() {
return i;
}
}
protected class GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
public Destination dest(String s) {
return new GDestination(s);
}
public Contents cont() {
return new Content();
}
}
class TestGoods {
public static void main(String[] args) {
Goods p = new Goods();
Contents c = p.cont();
Destination d = p.dest("Beijing");
}
}


also suggest similar

The first example compile an error
TestGoods.java: 1: Class Contents is public, should be declared in a file named Contents.java
public interface Contents {
^
TestGoods.java: 4: Class Destination is public, should be in a file named Destination.java sound
Ming
public interface Destination {
^
TestGoods.java: 7: Goods class is public, should be declared in a file named Goods.java
public class Goods {
^
3 errors

how to solve this ? a file can have only one public class bar. Not a main example of how that one class is public
------ Solution ---------------------------- ----------------
class Outer { 
        private int i = 10; 
        private int y = 8; 
 
        Outer() { 
                System.out.println("调用Outer构造方法:outer"); 
        } 
 
        public void sayMsg() { 
                System.out.println("Outer class!"); 
        } 
 
        class Inner { 
                int i = 1000; 
 
                Inner() { 
                        System.out.println("调用Inner构造方法:inner"); 
                } 
 
                void innerMsg() { 
                        System.out.println(">>>>>Inner class!"); 
                        sayMsg(); 
                        //访问内部类自己的成员i,也可以写成 this.i++ 
                        this.i++; 
                        //访问外部类的成员 i和y 
                        Outer.this.i++; 
                        y--; 
                } 
 
                int getI() { 
                        return i; 
                } 
        } 
 
        public void test() { 
                Inner in = new Inner(); 
                in.innerMsg(); 
        } 
 
        public int getI() { 
                return i; 
        } 
 
        public void setI(int i) { 
                this.i = i; 
        } 

 
public class Test1 { 
public static void main(String[] args) { 
Outer outer = new Outer(); 
outer.test(); 
System.out.println(outer.getI()); 
System.out.println("-------1--------"); 

Outer.Inner iner = outer.new Inner(); 
iner.innerMsg(); 
System.out.println(iner.getI()); 
System.out.println("-------2--------"); 

System.out.println(outer.getI()); 

}

This is a basic point in a file if you have several class files , only the main method of the class can be modified with the public .
------ Solution ---------------------------------------- ----
a file can have only one public class
------ Solution ------------------------- -------------------
you now put a few classes in a java file, then it can only have one public class , gave Out, on not to test,
that you should know while the main method to run main , must be to the publc class,
Your Test is now the default permissions , which does not run up .
------ For reference only ------------------- --------------------

+1
------ For reference only --------- ------------------------------
+1
----- - For reference only ---------------------------------------



then mean just write original blog author did not pay attention to this point is not it
------ For reference only ------------------- --------------------

meaning of the original Bowen did not notice or just write a

没有评论:

发表评论