`

recomprehend class and object

阅读更多
My second class,teacher xiong brief introduced some concepts about class and objects.It seems to be easier to understand what class and
objects are,but when you further comprehend those things you would find something unexpected always appear to frustrate you.
1.What constract one class?
(1)attribute and atrribute programming
   for example
//set the class' attributes
	//for the precious programming you should set the private attribute
	private String name;
	private int blood;
	
	//create the attribute grammars
	
	public void setName(String n){
		name=n;
	}
	
	public String getName(){
		return name;
	}
	
	public void setBlood(int n){
		blood=n;
	}
	
	public int getBlood(){
		return blood;
	}

     (2)class methods(object action)
        for example:
public void attack(ManagerHU a){
		int b=a.getBlood()-1;
		a.setBlood(b);
		System.out.println(a.getName()+"is being attacked and" +a.getName()+"'s blood is"+b);
	}


TIPS:
Why we define the private attribute?
A:One thing should always be kept in mind that class is imitating the reality things in the world.Everything has its own attribute and no one can easily change it.In order to contribute a harmony promgramming world we have to do this.

We create diffrent class depending on its own attributes and actions.So we have one formwork to create one which has just been listed above.

Q:Can we regard the class we creates as a data type?
A:Abusolutely we can.Take the String for example,so we can easily understand it.

Here is my Manager code
public class Manager {
	/**
	 * the entrance of the Procedure
	 */
	public static void main(String[] args){
		//crate the object
		Baron b=new Baron();
		ManagerHU m=new ManagerHU();
		//set the objects' attribute
		b.setName("LichKing");
		b.setBlood(5);
		m.setBlood(6);
		m.setName("MountKing");
		
		//now the PK begin
		while(b.getBlood()!=0&&m.getBlood()!=0){
			b.attack(m);
			m.attack(b);
		}
		if(b.getBlood()==0){
			System.out.println(m.getName()+" win");
		}
	}
}


key words:attribute  method
OK, so much for the class and object.Without them a vivid virtual world can't be created under our keyboard(or by coding).
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics