链接: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 classTest
{public static voidmain(String[] args)
{
Human aPerson= new Human(160);
System.out.println(aPerson.getHeight());
aPerson.growHeight(170);
System.out.println(aPerson.getHeight());
aPerson.repeatBreath(100);
}
}classHuman
{/*** constructor*/
public Human(inth)
{this.height =h;
System.out.println("I'm born");
}/*** accessor*/
public intgetHeight()
{return this.height;
}/*** mutator*/
public void growHeight(inth)
{this.height = this.height +h;
}/*** encapsulated, for internal use*/
private voidbreath()
{
System.out.println("hu...hu...");
}/*** call breath()*/
public void repeatBreath(intrep)
{inti;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
packagetext;interfaceCup
{void addWater(intw);void drinkWater(intw);
}class Text implementsCup
{public void addWater(intw)
{this.water = this.water +w;
System.out.println(water);
}public void drinkWater(intw)
{this.water = this.water -w;
System.out.println(water);
}public intwaterContent( )
{return this.water;
}public voidplay( )
{
System.out.println("la...la...");
}private int water = 0;public static voidmain(String args[ ])
{
Text obj= newText( );
obj.addWater(10);
obj.drinkWater(4);
}
}
java快速教程第6课组合
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/29/2982206.html
packagetext;public classText
{public static voidmain(String[] args)
{
Torch aTorch= newTorch( );
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);
}
}classBattery
{public void chargeBattery(doublep)
{if (this.power < 1.)
{this.power = this.power +p;
}
}public boolean useBattery(doublep)
{if (this.power >=p)
{this.power = this.power -p;return true;
}else{this.power = 0.0;return false;
}
}private double power = 0.0;
}classTorch
{/*** 10% power per hour use
* warning when out of power*/
public void turnOn(inthours)
{booleanusable;
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(inthours)
{this.theBattery.chargeBattery(hours*0.2);
}/*** composition*/
private Battery theBattery = newBattery();
}
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
packagetest;public classTest
{public static voidmain(String[ ] args)
{
System.out.println("hello world");
Woman aWoman= newWoman();
aWoman.growHeight(120);
System.out.println(aWoman.getHeight());
aWoman.breath();
aWoman.giveBirth();
}
}classHuman
{/*** contructor*/
public Human(intheight)
{this.height =height;
}publicHuman()
{
}/*** accessor*/
public intgetHeight()
{return this.height;
}/*** * mutator*/
public void growHeight(inth)
{this.height = this.height +h;
}/*** breath*/
public voidbreath()
{
System.out.println("hu...hu...");
}private intheight;
}class Woman extendsHuman
{/*** constructor*/
public Woman(inth)
{super(h); //base class construtor
System.out.println("Hello, Pandora!");
}publicWoman()
{
}/*** new method*/
publicHuman giveBirth()
{
System.out.println("Give birth");return (new Human(20));
}/*** override Human.breath( )*/
public voidbreath()
{super.breath( );
System.out.println("su...");
}
}
java快速教程第9课 类数据与类方法
学习网址:http://www.cnblogs.com/vamei/archive/2013/03/31/2988622.html
packagetest;public classTest
{public static voidmain(String[ ] args)
{
System.out.println(Human.getPopulation());
Human aPerson= new Human(160);
System.out.println(aPerson.getPopulation());
}
}classHuman
{/*** constructor*/
public Human(inth)
{this.height =h;
Human.population= Human.population +1;
}/*** accessor*/
public intgetHeight()
{return this.height;
}/*** mutator*/
public void growHeight(inth)
{this.height = this.height +h;
}/*** breath*/
public voidbreath()
{
System.out.println("hu...hu...");
}private intheight;/** static method, access population*/
public static intgetPopulation()
{returnHuman.population;
}private static intpopulation;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
packagetest;public classTest
{public static voidmain(String[] args)
{
Human aPerson= new Human(160);
Human dummyPerson=aPerson;
System.out.println(dummyPerson.getHeight());
aPerson.growHeight(20);
System.out.println(dummyPerson.getHeight());
}
}classHuman
{/*** constructor*/
public Human(inth)
{this.height =h;
}/*** accessor*/
public intgetHeight()
{return this.height;
}/*** mutator*/
public void growHeight(inth)
{this.height = this.height +h;
}private intheight;
}
java快速教程第12课 多态
学习网址:http://www.cnblogs.com/vamei/archive/2013/04/01/2992662.html
将一个衍生类引用转换为其基类引用,这叫做向上转换(upcast)或者宽松转换。