this,2 methods,overload_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > this,2 methods,overload

this,2 methods,overload

 2013/7/10 3:35:41  xuyi1994  程序员俱乐部  我要评论(0)
  • 摘要:Inthisclass,wefurthercomprehendedtheclass,objectandtheirmethods.AftertheclassIprogrammedonesimpleprogramming,afterthat,IthinkIhavemasteredthesyntaxformatandtheirapplications.Andwecanpasstheobject’svalueduringprogrammingwithoutthinking
  • 标签:
In this class, we further comprehended the class ,object and their methods. After the class I programmed one simple programming, after that , I think I have mastered the syntax format and their applications. And we can pass the object’s value during programming without thinking.

If we want to create an object. First we should define it and its methods.
First we use the construct method to define a new object.
className sth=new className();


Second we define the methods
public void(retrun value) methodName(value or address){}


The key word(this) has two usages
//overload Constructor Method
	public smallMonster(){
		//Method Call
		//use this to represent construct method
		this(2,5,"SNK",5);
	}
	
	public smallMonster(int ATK,int number,String name,int blood){
                  //this refer to the current object
		this.ATK=ATK;
		this.blood=blood;
		this.name=name;
		this.number=number;
	}


Two methods
1.construct method which can be overloaded
 
public smallMonster(int ATK,int number,String name,int blood){
		this.ATK=ATK;
		this.blood=blood;
		this.name=name;
		this.number=number;
	}


2.original method which can be overloaded two
 
public void attack(xiongBrother a){
		int q=a.getBlood()-(number*ATK-a.getDefence());
		a.setBlood(q);
        System.out.println("熊哥被"+number+"个小怪攻击了,剩余血量为  "+a.getBlood());
	}



Overload method(overload's name must be the same)
3.parameter number, parameter order, parameter type
(1).parameter type
public void attack(baron a){
		int b=a.getBlood()-5;
		a.setBlood(b);
		System.out.println(a.getName()+" is being attacked and  " +a.getName()+"'s blood is "+b);
	}
	//overload attack method
	public void attack(smallMonster s){
		int b=s.getBlood()-2;
		s.setBlood(b);
		
		if(s.getBlood()<0){
			s.number--;
		    i++;
		    s.setBlood(5);
		}
		System.out.println("smallMonster's number is "+s.number+"  ");
		System.out.println("NO."+i+" smallMonster's blood is "+s.getBlood());
	}


(2)parameter order
  
public void test(int i,String q){
       System.out.println("1");
}

public void test(String q,int i){
       System.out.println("2");
}

(3)parameter number
public void test(int i,String a){
		System.out.println("1");
	}
	public void test(int i){
		System.out.println("2");
	}


OK,for the entertainment,i write a story as the PK game's background.
To be honest, i do think the story is boring than funny.
public class Manager {
	/**
	 * the entrance of the Procedure
	 */
	public static void main(String[] args){
		//crate the object
		baron b=new baron();
		xiongBrother m=new xiongBrother();
		smallMonster s=new smallMonster();
		//set the objects' attribute
		b.setName("PrisidentZhao");
		b.setBlood(10);
		b.setDefence(0);
		m.setDefence(0);
		m.setBlood(100);
		m.setName("xiongBrother");
		
		//introduce the story's background
		System.out.println("Long long ago, there is a handsome programmer named xiongBrother");
		System.out.println("To maintain the world's harmony,he became a footman");
		System.out.println("xiongBrother's defence is"+m.getDefence()+"xiongBrother's HP is"+m.getBlood());
		System.out.println("PrisidentZhao's defence is"+b.getDefence()+"PrisidentZhao's HP is"+b.getBlood());
		System.out.println(s.number+"smallMonster defence is"+s.getDefence()+"smallMonster HP is"+s.getBlood());
		
		System.out.println("first he have to wipe out those smallMonster");
		//PK with smallMonster
		while(s.number!=0){
			m.attack(s);
			s.attack(m);
		}
		
		//now the PK begin
		while(b.getBlood()!=0&&m.getBlood()!=0){
			b.attack(m);
			m.attack(b);
		}
		if(b.getBlood()==0){
			System.out.println("Congratulation!  "+m.getName()+" win");
		}
	}
}



class smallMonster
public class smallMonster {
	//set monster's attributes
	private int blood;
	private String name;
	private int ATK;
	//this attribute can be changed by others
	public int number;
	
	
	//define the function of the class
	public void setName(String name){
		//use this(key word)
		this.name=name;//this refer to the current object
	}
	public String getName(){
		return name;
	}
	
	public void setBlood(int blood){
		this.blood=blood;
	}
	
	public int getBlood(){
		return blood;
	}
	
	public void setATK(int ATK){
		this.ATK=ATK;
	}
	
	//overload Constructor Method
	public smallMonster(){
		//Method Call
		//use this to represent construct method
		this(2,5,"SNK",5);
	}
	
	public smallMonster(int ATK,int number,String name,int blood){
		this.ATK=ATK;
		this.blood=blood;
		this.name=name;
		this.number=number;
	}
	
	public void attack(xiongBrother a){
		int q=a.getBlood()-(number*ATK-a.getDefence());
		a.setBlood(q);
        System.out.println("熊哥被"+number+"个小怪攻击了,剩余血量为  "+a.getBlood());
	}
	public int getDefence() {
		// TODO Auto-generated method stub
		return 0;
	}
  • 相关文章
发表评论
用户名: 匿名