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;
}
分享到:
相关推荐
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 by Gaston C. Hillar 2016 | ISBN: 1785885693 | English | 332 pages Get to grips with object-oriented programming in Swift to efficiently build powerful real-...
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 ...
Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...
Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...
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 ...
It addresses challenges faced in traditional methods, like information misreporting, communication inefficiencies, and document management overload. By leveraging modern technologies and best ...
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 ...
The transition from C to C++ brought with it the ability to define new data types and overload operators, which led to the creation of a robust `std::string` class in the C++ Standard Library. ...
**重载决议(Overload resolution)** 在C# 3.0中,重载决议规则已经更新,以处理lambda表达式和匿名方法。编译器会根据传入参数的类型和上下文来选择最合适的重载方法。 **对象和集合初始化器(Object and ...
Take advantage of inheritance, specialization, and the possibility to overload or override members Implement encapsulation, abstraction, and polymorphism Explore functional programming techniques ...
扩展方法 (Extension methods)** 扩展方法允许你在现有类型上添加新的方法,而无需继承或使用装饰者模式。这增强了代码的可复用性和模块化。声明扩展方法需使用`this`关键字,并放置在静态类中。 ```csharp public...
if (isset($this->methods[$methodName])) { $method = $this->methods[$methodName]; return call_user_func_array($method, $args); } else { throw new Exception("Method '$methodName' does not exist.");...
2. 作用域`public`, `protected`, `private`, 以及不写时的区别: - `public`:任何类都可以访问。 - `protected`:在同一包内和不同包的子类中可以访问。 - `private`:只有在定义它的类内部可以访问。 - 不写...
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 ...
2. **作用域public, protected, private及默认的区别**: - `public`:任何地方都能访问。 - `protected`:在同包内的类和不同包的子类中可访问。 - `private`:只在声明它的类内部可访问。 - 默认(无修饰符)...
2. 作用域 `public`, `protected`, `private`及默认的区别: - `public`:任何地方都能访问。 - `protected`:同一包内和不同包的子类可以访问。 - `private`:只有本类可以访问。 - 默认(无修饰符):同一包内...
#### 26.2 扩展方法 (Extension methods) 扩展方法是 C# 3.0 中另一个重要的新特性,它允许向现有类型添加新方法,而无需修改该类型的源代码。这为类库的设计提供了更大的灵活性,并且使得代码更加模块化和可重用。...
- **this和super关键字**: this指向当前对象的引用,super用于调用父类的构造器和方法。 - **单例模式(Singleton)**: 确保一个类只有一个实例,并提供一个全局访问点。 ### Java程序设计基础 - **equals和==**: ...
#### 2. 求序列 `s` 的值 **知识点:** - **循环结构:** 使用循环结构来构建所需的序列。 - **字符串拼接与转换:** 将数字转换为字符串进行拼接,并再转换回数值进行计算。 **示例代码框架:** ```java import java...