2013.07.07
上课内容:类和对象
回顾一下上节课内容,上节课我们讲了类和对象的定义,还有就是类中属性和方法的设置,以及方法的调用。
这节课我们进行进一步的讲解:
我们知道类是有属性和方法构成,而方法又是由普通方法和构造方法构成。普通方法表示的是对象的行为,构造方法表示的是实例化对象的方法。
什么叫实例化对象的方法,简单地说,就是给对象进行初始化,我们在创建对象时,对象会有很多属性,然而这些属性必须通过set方法才能够对属性进行初始化。
显然,这样会很麻烦,我们想在new一个新对象时就对属性有一个初始化,拥有默认的属性值,这时就要使用到构造方法。
构造方法的语法:
public 类名(数据类型 形参名,...){
方法体
}
普通方法的语法:
public 返回值数据类型 方法名(数据类型 形参名,...){
方法体
}
这里构造方法和普通语法的区别有两点:
1.返回值不同,构造方法不用返回值而普通方法需要返回值,即使无返回值也要输入void。
1.类名和方法名不相同,类名必须是文件名。
我们以上节课讲的学生为例:
//创建一个构造方法
class Student{
private string name;
public Student(String n){
name=n;
}
}
//调用构造方法:
Student stu = new Student("Jack");
了解了构造方法,我们下面要讲的是方法的重载,所谓方法的重载,就是让类以统一的方式处理不同数据类型的一种手段。
在方法的调用过程中,我们往往需要输入不同个数的参数名,不同类型的参数名,或者不同顺序的参数名。
要实现这种需求,就需要用到方法的重载。
方法的重载有两个条件:
1.方法的名称必须相同,就是说只有方法名相同才能考虑到方法的重载。
2.方法所带的参数个数、参数的类型、参数的顺序必须至少有一项不相同。
在建立过方法重载后,我们就需要根据需要代入不同的参数。
下面有一个常用的技巧:this的使用技巧
在定义方法中,若形参名与类中的定义的属性名相同,系统就会默认在这个方法中所出现的与形参名相同的代码全为属性名,这叫做就近原则。
为了避免这种情况出现,我们使用关键词this,从而能够调用属性名称。
this还有另外一种用法,就是调用构造函数,可以在重载的前提下达到进行初始化的目的。
我们还是以Student为例:
public class Student{
private string name;
public Student(){
this("Jack");
}
public Student(string name){
this.name=name;
}
}
下面我们要讲的是值传递和引用传递:
值传递:java在这点与C很像,当函数传进参数时,只是值传递,在函数中改变形参并不会影响实参的数值,进行值传递时,会将实参的值赋给形参,然后形参在函数中进行改变,但这是不同的变量,并不会影响实参的值。
举个例子:
//我们定义一个函数
public void swap(int x,int y){
int t;
t=x;x=y;y=t;
}
//我们在主函数中进行交换
int x=1,y=2;
System.out.println("x="+x+",y="+y);
swap(x,y);
System.out.println("x="+x+",y="+y);
结果发现两次输出结果相同,原因就是函数传入参数只是值传递,并不会改变形参的数值。
引用传递:所谓引用传递,就是指当在一个类中定义不同的对象时,传入的是对象的地址,而不是对象的值,所以不同的对象可以赋值,我们用例子来说明。
引用我们已创建了Student类:
Student stu1 = new Student("Jack");
Student stu2 = new Student("Linda");
stu1 = stu2;
此时,stu2的地址赋给了stu1,原stu2的在堆空间中的数据被GC(销毁+释放),现在stu2与stu1所指的地址相同,同位包含Jack的堆空间地址。
本期的练习沿用了上节课的练习,并加入了构造方法和方法重载等新的知识点。
目标:回合制游戏:Teemo和Creeps进行战斗,直到一方的血量为0时结束战斗,输出谁胜利了!若Teemo死了,则战斗结束!
Teemo再和Nashor进行战斗,直到一方的血量为0时结束战斗,输出谁胜利了!
附程序:
package cn.tth.pratice0706;
public class Scence {
/**
* @author TTh
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Teemo te = new Teemo();
Creeps cr = new Creeps();
Nashor na = new Nashor();
boolean flags=true;
while(flags){
te.AttackCreep(cr);
int crhp=(int)cr.getHp();
if(cr.getHp()<=0){
System.out.println("Creeps has been down.");
break;
}else{
System.out.print("Creeps hp="+crhp+" ");
cr.Attacked(te);
int tehp=(int)te.getHp();
if(te.getHp()<=0){
flags=false;
System.out.println("");
System.out.println("Exquited!");
}else{
System.out.println("Teemo's hp="+tehp);
}
}
}
while(flags){
te.AttackNashor(na);
int nahp=(int)na.getHp();
if(na.getHp()<=0){
System.out.println("Nashor has been down.");
break;
}else{
System.out.print("Nashor's hp="+nahp+" ");
na.Attacked(te);
int tehp=(int)te.getHp();
if(te.getHp()<=0){
flags=false;
System.out.println("");
System.out.println("Shut down!");
}else{
System.out.println("Teemo's hp="+tehp);
}
}
}
}
}
package cn.tth.pratice0706;
public class Teemo {
private int atk;
private int def;
private double hp;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public double getHp() {
return hp;
}
public void setHp(double hp) {
this.hp = hp;
}
public Teemo(int atk,int def,double hp){
this.atk=atk;
this.def=def;
this.hp=hp;
}
public Teemo(){
this(200,50,1500);
}
public void AttackNashor(Nashor na){
na.setHp(na.getHp()-atk*(1-na.getDef()/100.0));
}
public void AttackCreep(Creeps cr){
cr.setHp(cr.getHp()-atk*(1-cr.getDef()/100.0));
}
}
package cn.tth.pratice0706;
public class Nashor {
private int atk;
private int def;
private double hp;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public double getHp() {
return hp;
}
public void setHp(double hp) {
this.hp = hp;
}
public Nashor(int atk,int def,int hp){
this.atk=atk;
this.def=def;
this.hp=hp;
}
public Nashor(){
this(100,25,4000);
}
public void Attacked(Teemo te){
te.setHp(te.getHp()-atk*(1-te.getDef()/100.0));
}
}
package cn.tth.pratice0706;
public class Creeps {
private int atk;
private int def;
private double hp;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public double getHp() {
return hp;
}
public void setHp(double hp) {
this.hp = hp;
}
public Creeps(int atk,int def,double hp){
this.atk=atk;
this.def=def;
this.hp=hp;
}
public Creeps(){
this(100,15,500);
}
public void Attacked(Teemo te){
te.setHp(te.getHp()-atk*(1-te.getDef()/100.0));
}
}