12 2011 档案

摘要:当我们在子类中重写父类的方法的时候,子类的权限不能低于父类的权限。 比如: class A { access_specifier_1 void f() { System.out.printf (“AAAA\n”); } } class B extends A { access_specifier_2 void f() { System.out.printf (“BBBB\n”); } } access_specifier_1access_specifier_2ResultpublicpublicOKprotectedpublicOKdefaultprotectedOKprivatepriv.. 阅读全文
posted @ 2011-12-02 07:52 allenbackpacker 编辑
摘要:1)方法的重写overriding和重载overloading是java多态性的不同表现. (2)重写overriding是父类与子类之间的多态性的一种表现,重载是一个类中多态性的表现。如果子类中定义方法与其父类有相同的名称和参数,我们说该方法被重写,子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被屏蔽了。 (3)如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法重载。重载的方法可以改变返回值的类型。 现实中的例子:对比上面看,比如一老爹是木工。儿子继承老爹也是木工。 老爹有个做柜子的独特技术(方法),只需要用到工具锤子和木头(参 阅读全文
posted @ 2011-12-02 07:51 allenbackpacker 编辑
摘要:See a sample: ———-TestOverride.java———– package ch.allenstudy.newway02;public class TestOverride { public static void main(String[] zhangsan) { B bb = new B(); bb.f(); } } class A { public void f() { System.out.printf(“AAAA\n”); } } class B extends A { public void f() //this is OVERRIDE the method f 阅读全文
posted @ 2011-12-02 07:51 allenbackpacker 编辑
摘要:supper summary: 1. 每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有无参的构造函数,那么在编译的时候就会报错。 2. super();语句可以写,也可以不写,不写的化,系统会自动调用的 3. 如果显示写出来的话,编译器要求该语句前面不能加任何语句,也就是说该语句必须保证是第一条语句 4. super()也可以改为super(2); 但前提是父类必须有带一个参数的构造函数,否则也会报错 5. 一个子类任何一个构造函数中都只能出现一个 super(实参列表) 6. 调用父类的构造器的语句必须借助于 super,不能直接写父类的类名。 阅读全文
posted @ 2011-12-02 07:49 allenbackpacker 编辑
摘要:1。实例化一个对象的时候一定会调用类的 Constructor。而且一定只调用一个 Constructor,而不是多个 2。如果一个类没有写 Constructor, 那么当你创建对象的时候会自动创建并调用一个参数为空的Constructor。 如果一个类已经写了带参数的Constructor,创建对象的时候就不会创建并调用参数为空的 Constructor。 阅读全文
posted @ 2011-12-02 07:48 allenbackpacker 编辑
摘要:Java 只支持单继承,不支持多继承 单继承就是一个类只能由一个父类;多继承就是一个类可以有多个父类。 子类可以继承父类所有的成员变量和成员方法,但子类永远无法继承父类的构造器(Constructor)。在子类构造方法中可以使用语句 Super(参数列表)调用父类的构造方法。 So, let’s see: subclass can’t inherit Superclass’ constructor. Assume we have below: ——————– class A { private int i,j,k; public A() { 这里给 i,j,k 赋值 } } class B e 阅读全文
posted @ 2011-12-02 07:48 allenbackpacker 编辑
摘要:Explanation: Objects A, B, C are allocated into memory. Then B is unused. The space of B is released. Then D is coming, and is looking for a memory to store. B’s space is released, but is too small to D, so D find another place below C. Later, because of the small space of B left, no variables can u 阅读全文
posted @ 2011-12-02 07:47 allenbackpacker 编辑
摘要:Java 只支持单继承,不支持多继承 单继承就是一个类只能有一个父类;多继承就是一个类可以有多个父类。 子类可以继承父类所有的成员变量和成员方法,但子类永远无法继承父类的构造器(Constructor)。在子类构造方法中可以使用语句 Super(参数列表)调用父类的构造方法。 阅读全文
posted @ 2011-12-02 07:46 allenbackpacker 编辑
摘要:when to select “extends”? Is “B” an “A”? If yes, let “B” be the subclass of “A”. B是一个A吗? 如果是,则让B做A的子类。Common mistake: A有一个B吗? 比如:让汽车轮子成为汽车的子类是错误的。因为“汽车轮子”不是一个“汽车”。 阅读全文
posted @ 2011-12-02 07:43 allenbackpacker 编辑
摘要:The private properties/methods of the Superclass cannot be accessed by subclass. The non-private properties/methods of the Superclass can be access by subclass’s objects directly.总结: 1. “private” can’t be “extends” 2. “private” 物理上已经被继承过来了,只不过逻辑上不能访问他。所以继承必须慎重,否则浪费内存空间。 阅读全文
posted @ 2011-12-02 07:42 allenbackpacker 编辑
摘要:“Extends” simulates the relationship between the things in real world. 父类: Superclass 子类: Subclass A Java superclass is a class which gives a method or methods to a Java subclass. A Java class may be either a subclass, a superclass, both, or neither!———-TestExtends.java———- package ch.allenstudy.new 阅读全文
posted @ 2011-12-02 07:41 allenbackpacker 编辑
摘要:How to make sure a class only can generate one object? First, let’s think about this: ———-D.java————- package ch.allenstudy.newway02; public class D { } class A { } class C { public int i = 10; //i 是 C 的属性 } ————————- 这个没有问题吧, i 是 C 的属性。 Let’s add one more line: ———-D.java————- package ch.allenstudy 阅读全文
posted @ 2011-12-02 07:40 allenbackpacker 编辑
摘要:For weeks I don’t touch guitar. By accident I know that Hans is the professional of guitar! Amazing! He took the guitar to me for some practice here. The guitar is very good voice. I feel it’s 单板, maybe 红木.Thanks very much, Hans! 阅读全文
posted @ 2011-12-02 07:37 allenbackpacker 编辑
摘要:How to know how many objects has been generated for a class? We need use the “static” knowledge. Every time you new an object, you will call the constructor(s) of the class. And the “static” property can be updated every time. So, see below sample: —————————— package ch.allenstudy.newway01; public c 阅读全文
posted @ 2011-12-02 07:28 allenbackpacker 编辑
摘要:“this” is only for dynamic method. We don’t need it for static properties/methods, because static properties/methods are already shared in the memory. The reason we use “this” is to distinguish the values for different objects. 阅读全文
posted @ 2011-12-02 07:27 allenbackpacker 编辑
摘要:只有非 private (public, protected, or no identifier) 的 static 成员 (成员指属性和方法)才能通过类名的方式访问。 static 只是表明该成员具有了可以通过类名访问的潜在特征,但是否可以通过类名访问 (class.xxx),还必须具备一个条件:该成员必须是非private Sample: ==================== package ch.allenstudy.newway01;public class Recursion1 { public static void main(String[] args) { System.o 阅读全文
posted @ 2011-12-02 07:26 allenbackpacker 编辑
摘要:Sample first:———————————- package ch.allenstudy.newway01; public class TestStatic { public static void main(String[] args) { A aa1 = new A(); A aa2 = new A();aa1.i = 20; //Here, object “aa1″ sets its property i’s value to “20″. aa2.show(); //Here, object “aa2″ call the show() method which calls its 阅读全文
posted @ 2011-12-02 07:24 allenbackpacker 编辑
摘要:静态方法可以直接用类名点出来方法,而普通方法需要创建类的对象后才能调用! 静态的方法和变量会调用时在内存生成一个唯一的标示,你可以理解成在物理内存中给静态一个位子,这样的话在调用的时候可以直接找到,而且会节省内存,但是如果你声明的静态过多的话那么每一个都会在内存有一个位子,那么你就没有资源运行别的,会报内存溢出! 普通方法是由java的gc机制来控制,可能同一个对象或变量在使用的过程中,这个时间的在内存占了一个位子,而上个时间的还没有从内存中删除,这样的话就可能有2个一样的在内存中,这个2个一样东西只是内容值一样,但是内存值不一样,你可以用”==”和”equals”来查看(这个只适用对象和St 阅读全文
posted @ 2011-12-02 07:20 allenbackpacker 编辑
摘要:As usual, a sample first. ========================== package ch.allenstudy.newway01;public class ThisPointer {public static void main(String[] args) { A aa1 = new A(10); A aa2 = new A(20); aa1.show(); aa2.show(); }}class A { public int i;public A(int a) { i = a; }public void show() { System.out.prin 阅读全文
posted @ 2011-12-02 07:12 allenbackpacker 编辑
摘要:Ctrl+Shift+F to format code in Eclipse 阅读全文
posted @ 2011-12-02 07:11 allenbackpacker 编辑
摘要:Let’s write a java file first. File name is Gouzaohanshu.java notice: the main() must be in the class name which is same as the file name, i.e. Gouzaohanshu ============================ package ch.allenstudy.newway01;class Gouzao{ private int i,j; public void set(int a, int b) { i = a; j = b; } publ 阅读全文
posted @ 2011-12-02 07:10 allenbackpacker 编辑
摘要:This post is to makde a NullPointerException and explain a little bit. See the capture first.U can see that the value of string[1] is NULL, then the code wants to use charAt() method to call it. Finally there’s a NullPointerException error. This error is always because we want to operate an object w 阅读全文
posted @ 2011-12-01 21:37 allenbackpacker 编辑
摘要:This post shows how to create a StackOverflowError with a recursion. Let’s look at the error message first: Exception in thread “main” java.lang.StackOverflowError at ch.allenstudy.newway01.Recursion1.f(Recursion1.java:9) at ch.allenstudy.newway01.Recursion1.g(Recursion1.java:14) at ch.allenstudy.ne 阅读全文
posted @ 2011-12-01 21:35 allenbackpacker 编辑
摘要:Recursion: 递归 P(n)= P(n-1)* n 阅读全文
posted @ 2011-12-01 21:34 allenbackpacker 编辑
摘要:Private methods/properties (priviate int a;) can’t be accessed from outside of the class. Protected (protected int a;)can be accessed in the class or from its sub-class. Public (public int a;)can be accessed from any places. The methods/properties without any identifier (int a) can be access by the 阅读全文
posted @ 2011-12-01 21:33 allenbackpacker 编辑
摘要:Here attached an article about heap (堆) and stack (栈). HeapAndStack Java will store all things into system memory (system memory is physical mem + virtual mem), then it’s the OS to decide they go to physical memory or hard disk (virtual memory) later.堆是先进先出,而栈是先进后出. But in Java, not that obvious dif 阅读全文
posted @ 2011-12-01 08:18 allenbackpacker 编辑
摘要:Use -7 as sample.int a = -7; //declaration, notice that int is 4 bytes, so 32 bits. The bit representation will be in 2’s complement form.Steps to find 2’s Complement. Take binary representation of positive value (in this case, it is 7) 0000 0000 0000 0000 0000 0000 0000 0111Take 1’s Complement of i 阅读全文
posted @ 2011-12-01 08:12 allenbackpacker 编辑
摘要:The dinner we had tonight, Elisabeth and I call it “the mixture of West And East”. Why? You will know this after watch below picture.You can see that the food is mixed with very Chinese native food (allen cooked) and very Swiss native food (Elisabeth cooked). Do more explanation to you with this pic 阅读全文
posted @ 2011-12-01 08:02 allenbackpacker 编辑
摘要:Question desc: I am using Eclipse for java file. I write below code, and then RUN, but the result is garbage characters. How to fix it? —————————— package ch.allenstudy.newway01; public class TestPrintf { public static void main(String[] args) { // 定义一些变量,用来格式化输出。 double d = 345.678; String s = “Hel 阅读全文
posted @ 2011-12-01 07:51 allenbackpacker 编辑