Java快速教程--vamei 学习笔记(基础篇)
链接:http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html
java快速教程第1课 从HelloWorld到面向对象
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/14/2958654.html
java快速教程第2课 方法与数据成员
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/25/2964430.html
java快速教程第3课 构造器与方法重载
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/26/2981728.html
java快速教程第4课 封装与接口
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/27/2982209.html
public class Test { public static void main(String[] args) { Human aPerson = new Human(160); System.out.println(aPerson.getHeight()); aPerson.growHeight(170); System.out.println(aPerson.getHeight()); aPerson.repeatBreath(100); } } class Human { /** * constructor */ public Human(int h) { this.height = h; System.out.println("I\'m born"); } /** * accessor */ public int getHeight() { return this.height; } /** * mutator */ public void growHeight(int h) { this.height = this.height + h; } /** * encapsulated, for internal use */ private void breath() { System.out.println("hu...hu..."); } /** * call breath() */ public void repeatBreath(int rep) { int i; for(i = 0; i < rep; i++) { this.breath(); } } private int height; // encapsulated, for internal use }
java快速教程第5课 实施接口
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/29/2982206.html
package text; interface Cup { void addWater(int w); void drinkWater(int w); } class Text implements Cup { public void addWater(int w) { this.water = this.water + w; System.out.println(water); } public void drinkWater(int w) { this.water = this.water - w; System.out.println(water); } public int waterContent( ) { return this.water; } public void play( ) { System.out.println("la...la..."); } private int water = 0; public static void main(String args[ ]) { Text obj = new Text( ); obj.addWater(10); obj.drinkWater(4); } }
java快速教程第6课组合
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/29/2982206.html
package text; public class Text { public static void main(String[] args) { Torch aTorch = new Torch( ); System.out.println("Charge: 2 hours"); aTorch.charge(2); System.out.println("First Turn On: 3 hours"); aTorch.turnOn(3); System.out.println("Second Turn On: 3 hours"); aTorch.turnOn(3); } } class Battery { public void chargeBattery(double p) { if (this.power < 1.) { this.power = this.power + p; } } public boolean useBattery(double p) { if (this.power >= p) { this.power = this.power - p; return true; } else { this.power = 0.0; return false; } } private double power = 0.0; } class Torch { /** * 10% power per hour use * warning when out of power */ public void turnOn(int hours) { boolean usable; usable = this.theBattery.useBattery(hours*0.1); if ( usable != true ) { System.out.println("No more usable, must charge"); } } /** * 20% power per hour charge */ public void charge(int hours) { this.theBattery.chargeBattery(hours*0.2); } /** * composition */ private Battery theBattery = new Battery(); }
java快速教程第7课 包的建立和使用
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/29/2982206.html
java快速教程第8课 继承
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/29/2982232.html
package test; public class Test { public static void main(String[ ] args) { System.out.println("hello world"); Woman aWoman = new Woman(); aWoman.growHeight(120); System.out.println(aWoman.getHeight()); aWoman.breath(); aWoman.giveBirth(); } } class Human { /** * contructor */ public Human(int height) { this.height = height; } public Human() { } /** * accessor */ public int getHeight() { return this.height; } /** * * mutator */ public void growHeight(int h) { this.height = this.height + h; } /** * breath */ public void breath() { System.out.println("hu...hu..."); } private int height; } class Woman extends Human { /** * constructor */ public Woman(int h) { super(h); //base class construtor System.out.println("Hello, Pandora!"); } public Woman() { } /** * new method */ public Human giveBirth() { System.out.println("Give birth"); return (new Human(20)); } /** * override Human.breath( ) */ public void breath() { super.breath( ); System.out.println("su..."); } }
java快速教程第9课 类数据与类方法
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/31/2988622.html
package test; public class Test { public static void main(String[ ] args) { System.out.println(Human.getPopulation()); Human aPerson = new Human(160); System.out.println(aPerson.getPopulation()); } } class Human { /** * constructor */ public Human(int h) { this.height = h; Human.population = Human.population +1; } /** * accessor */ public int getHeight() { return this.height; } /** * mutator */ public void growHeight(int h) { this.height = this.height + h; } /** * breath */ public void breath() { System.out.println("hu...hu..."); } private int height; /* * static method, access population */ public static int getPopulation() { return Human.population; } private static int population; private static boolean is_mammal = true; }
java快速教程第10课 接口的继承与抽象类
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/31/2982240.html
java快速教程第11课 对象引用
学习网址:http://www.cnblogs.com/vamei/archive/2013/04/01/2992484.html
package test; public class Test { public static void main(String[] args) { Human aPerson = new Human(160); Human dummyPerson = aPerson; System.out.println(dummyPerson.getHeight()); aPerson.growHeight(20); System.out.println(dummyPerson.getHeight()); } } class Human { /** * constructor */ public Human(int h) { this.height = h; } /** * accessor */ public int getHeight() { return this.height; } /** * mutator */ public void growHeight(int h) { this.height = this.height + h; } private int height; }
java快速教程第12课 多态
学习网址:http://www.cnblogs.com/vamei/archive/2013/04/01/2992662.html
将一个衍生类引用转换为其基类引用,这叫做向上转换(upcast)或者宽松转换。