- 类(设计图):是对对象共同特征的描述,对具有相同特征和作用的对象的抽象。
- 对象:是真实存在的具体实例,任何一个具体的事物或者物体就是一个对象。
- 结论:在Java中,必须先设计类,才能创建对象使用。
- 面向对象编程:根据生活中的对象在Java中设计对应的类。
一. 程序:小夜正在学习Java课程,学分增加了5分,现在的学分是90!
分析:
学生类(小夜学生对象):
属性/变量/特征:名字,学分
方法/行为/函数:学习
课程类(Java课程对象):
属性/变量/特征:名字,学分
方法/行为/函数:
package com.gch.oop;
/**
定义课程类
*/
public class Course {
private String name;
private int score;
public Course(String name, int score) {
this.name = name;
this.score = score;
}
public Course() {
}
/**
* 定义set方法,初始化属性
* 定义get方法,获取属性
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package com.gch.oop;
/**
定义学生类
*/
public class Student {
private String name;
private int score;
/**
* 定义学习方法
* @param c:学习的课程
*/
public void study(Course c){
setScore(this.getScore() + c.getScore());
System.out.println(this.name + "正在学习" + c.getName() + "课程,学分增加了" + c.getScore() + "分," +
"现在的学分是" + this.getScore() + "!");
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public Student() {
}
/**
* 定义set方法,初始化属性
* 定义get方法,获取属性
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
package com.gch.oop;
/**
定义主函数
*/
public class Manage {
public static void main(String[] args) {
Student s = new Student("小夜",85);
Course c = new Course("Java",5);
s.study(c);
}
}
二.回合制游戏
1.后羿正在攻击安其拉,安其拉掉血xx,剩余血量xx;
安其拉正在攻击后羿,后羿掉血xx,剩余血量xx;
2.后羿正在攻击安其拉,安其拉掉血xx,剩余血量xx;
安其拉正在攻击后羿,后羿掉血xx,剩余血量xx;
.......
判断输赢?
package com.gch.oop01;
/**
定义英雄类
*/
public class Hero {
private String name; // 名字
private int ack; // 攻击力
private int hc; // 血量
public Hero(String name, int ack, int hc) {
this.name = name;
this.ack = ack;
this.hc = hc;
}
public Hero() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAck() {
return ack;
}
public void setAck(int ack) {
this.ack = ack;
}
public int getHc() {
return hc;
}
public void setHc(int hc) {
this.hc = hc;
}
}
package com.gch.oop01;
/**
定义后羿类
*/
public class HouYi extends Hero{
/**
后羿攻击安其拉的方法
*/
public void attack(AnQiLa aql){
aql.setHc(aql.getHc() - this.getAck());
System.out.println(this.getName() + "正在攻击" + aql.getName() + ","
+ aql.getName() + "掉血" + this.getAck() + ",剩余血量" + aql.getHc());
}
/**
调用父类的有参构造器,初始化继承自父类的数据
*/
public HouYi(String name,int atk,int hc){
super(name,atk,hc);
}
}
package com.gch.oop01;
/**
定义安其拉类
*/
public class AnQiLa extends Hero {
/**
安其拉攻击后羿的方法
*/
public void attack(HouYi hy){
hy.setHc(hy.getHc() - this.getAck());
System.out.println(this.getName() + "正在攻击" + hy.getName() + ","
+ hy.getName() + "掉血" + this.getAck() + ",剩余血量" + hy.getHc());
}
/**
调用父类有参构造器,初始化继承自父类的数据
*/
public AnQiLa(String name,int atk,int hc){
super(name,atk,hc);
}
}
package com.gch.oop01;
public class Manage {
/**
定义主函数
*/
public static void main(String[] args) {
HouYi hy = new HouYi("后羿",8,100);
AnQiLa aql = new AnQiLa("安其拉",6,120);
// 互相攻击
while(hy.getHc() > 0 && aql.getHc() > 0){
hy.attack(aql);
if(aql.getHc() <= 0){
System.out.println("安其拉死亡,后羿获胜!");
break;
}
aql.attack(hy);
if(hy.getHc() < 0){
System.out.println("后羿死亡,安其拉获胜!");
break;
}
}
}
}