Java Constructor From Thinking in Java C7
2015-01-17 00:38
260 查看
Case 1: Inheritance && Basic constructor
Codes:
package constructorTest;
class Art{
Art(){
System.out.println("Art constructed");
}
}
class Drawing extends Art{
Drawing(){
System.out.println("Drawing constructed");
}
}
public class Cartoon extends Drawing{
Cartoon(){
System.out.println("Cartoon constructed");
}
public static void main(String[] args){
Cartoon cartoon = new Cartoon();
}
}
Output:
Art constructed
Drawing constructed
Cartoon constructed
Codes:
package constructorTest;
class Game{
Game(int i){
System.out.println("Game constructed: "+ i);
}
Game(){
System.out.println("Game1 constructed: ");
}
}
public class Chess extends Game {
Chess(int i) {
super(i);
System.out.println("Chess Constructed:" +i);
// TODO Auto-generated constructor stub
}
public static void main(String[] args){
Chess chess = new Chess(3);
}
}
Output:
Game constructed: 3
Chess Constructed:3
Case3: delete super(i)
output:
Game1 constructed:
Chess Constructed:3
Codes:
package constructorTest;
class Art{
Art(){
System.out.println("Art constructed");
}
}
class Drawing extends Art{
Drawing(){
System.out.println("Drawing constructed");
}
}
public class Cartoon extends Drawing{
Cartoon(){
System.out.println("Cartoon constructed");
}
public static void main(String[] args){
Cartoon cartoon = new Cartoon();
}
}
Output:
Art constructed
Drawing constructed
Cartoon constructed
Codes:
package constructorTest;
class Game{
Game(int i){
System.out.println("Game constructed: "+ i);
}
Game(){
System.out.println("Game1 constructed: ");
}
}
public class Chess extends Game {
Chess(int i) {
super(i);
System.out.println("Chess Constructed:" +i);
// TODO Auto-generated constructor stub
}
public static void main(String[] args){
Chess chess = new Chess(3);
}
}
Output:
Game constructed: 3
Chess Constructed:3
Case3: delete super(i)
output:
Game1 constructed:
Chess Constructed:3
相关文章推荐
- Thinking in Java from Chapter 15
- Thinking in Java from Chapter 7
- Thinking in Java from Chapter 10
- Thinking in Java from Chapter 21
- How a garbage collector works----From Thinking in Java the 4th edition
- Thinking in Java from Chapter 11
- 简单了解Java枚举类型 —— From Thinking In Java(Fourth Eition)
- java -- 性能(from thinking in java)
- Reading "Thinking in Java" #3
- java 线程[摘抄至《Thinking in Java》]
- 《Thinking in Java》读书笔记(二)
- 大名鼎鼎的thinking in java 作者的主页。
- 【Notes】《Thinking in Java》【Chapter 11】
- 很多人知道《Thinking in Java》吧,那么《Thinking in C#》呢?
- 《thinking in java》学习手记(四)
- Thinking in Java 读书笔记 —— 1.Introducation to Object
- Thinking in Java 读书笔记 —— 1.Introducation to Object
- thinking in java 第三版 学习笔记(第2章 一切都是对象)
- Reading "Thinking in Java" #5
- Reading "Thinking in Java" #4