`

this,2 methods,overload

阅读更多
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;
	}
分享到:
评论

相关推荐

    JavaScript Objects Functions and Arrays Explained

    "If you are looking for a source that describes the most common methods from the JavaScript library, this is it. This is a great reference book." How many times have you attempted to learn JavaScript,...

    Social Recommender Systems Tutorial - 2011.part2

    The goal of this tutorial is to expose participants to the current research on social recommender systems (i.e., recommender systems for the social web). Participants will become fabvbamiliar with ...

    Object Oriented Programming with Swift 2

    Object Oriented Programming with Swift 2 by Gaston C. Hillar 2016 | ISBN: 1785885693 | English | 332 pages Get to grips with object-oriented programming in Swift to efficiently build powerful real-...

    Social Recommender Systems Tutorial - 2011

    The goal of this tutorial is to expose participants to the current research on social recommender systems (i.e., recommender systems for the social web). Participants will become fabvbamiliar with ...

    Swift.3.Object.Oriented.Programming.2nd.epub

    Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...

    Swift 3 Object Oriented Programming, 2nd Edition [PDF]

    Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    C# 语言规格说明(English Edition第五版)

    7.6.7 This access 170 7.6.8 Base access 170 7.6.9 Postfix increment and decrement operators 171 7.6.10 The new operator 172 7.6.10.1 Object creation expressions 172 7.6.10.2 Object initializers 173 ...

    二级减速器课程设计说明书reducer design specification.doc

    The extemal outline size of this scheme is large, with good shock absorption capacity, low manufacturing, stability accuracy with low cost, and overload protection. But because the gear relative to ...

    C#语言规范 版本 4.0(English)

    Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they ...

    Swift 3 Object-Oriented Programming

    Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...

    CLR via C# 第4版 英文PDF

    Operator Overload Methods 191 Operators and Programming Language Interoperability 193 Conversion Operator Methods 195 Extension method 198 Rules and guidelines ....,200 Extending Various Types with ...

    java 面试题 总结

    换言之,很可能数个使用者在执行某个 Stateless Session Bean 的 methods 时,会是同一个 Bean 的 Instance 在执行。从内存方面来看, Stateful Session Bean 与 Stateless Session Bean 比较, Stateful Session ...

    超级有影响力霸气的Java面试题大全文档

    换言之,很可能数个使用者在执行某个 Stateless Session Bean 的 methods 时,会是同一个 Bean 的 Instance 在执行。从内存方面来看, Stateful Session Bean 与 Stateless Session Bean 比较, Stateful Session ...

Global site tag (gtag.js) - Google Analytics