Java面向对象实例
1. 奥特曼打小怪兽
public class Ultraman {
private String name;
private int hp;//生命值
private int mp;//魔法值
public Ultraman(String name,int hp,int mp){
this.name = name;
this.hp = hp;
this.mp = mp;
}
public String getName() {
return name;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp < 0 ? 0 : hp;
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
public String info(){
return String.format("%s奥特曼 - 生命值:%d 魔法值:%d",name, hp, mp);
}
public void attack(Monster m){
int injury = (int) (Math.random() * 11 + 10);
m.setHp(m.getHp() - injury);
}
public void hugeAttack(Monster m){//究极必杀技
if (mp >= 40) {
int injury = m.getHp() / 4 * 3 > 50 ? m.getHp() / 4 * 3 : 50;
m.setHp(m.getHp() - injury);
mp -= 40;
}
}
public void magicalAttack(Monster[] mArray){
if (mp >= 15) {
for (Monster m : mArray) {
if (m.getHp() > 0) {
int injury = (int) (Math.random() * 11 + 15);
m.setHp(m.getHp() - injury);
}
}
mp -= 15;
}
}
public void resumeMp() {
mp += 4;
}
}
public class Monster {
private String name;
private int hp;
//构造器
public Monster(String name,int hp){
this.name = name;
this.setHp(hp);
}
public String info(){
return String.format("%s怪兽 - 生命值:%d",name, getHp());
}
//小怪兽的攻击方法
public void against(Ultraman u){
int injury = (int) (Math.random() * 11 + 5);
u.setHp(u.getHp() - injury);
}
public String getName() {
return name;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {//修改器 - 修改生命值
this.hp = hp < 0 ? 0 : hp;
}
}
public class AttackMain {
public static Monster selectOne(Monster[] mArray){//用了static,不用创建对象,可直接调用
Monster temp = null; //没有加static的方法,必须创建了对象才能调用
do {
int randomIndex = (int) (Math.random() * mArray.length);
temp = mArray[randomIndex];
} while (temp.getHp() == 0);
return temp;
}
public static void showMonstersInfo(Monster[] mArray){
for (Monster monster : mArray) {
System.out.println(monster.info());
}
}
public static boolean isAllDead(Monster[] mArray){
for (Monster monster : mArray) {
if (monster.getHp() > 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Ultraman u = new Ultraman("迪迦", 250, 100);
System.out.println(u.info());
Monster m1 = new Monster("阿尔法", 130);
Monster m2 = new Monster("jack", 130);
Monster m3 = new Monster("ben", 130);
Monster m4 = new Monster("tony", 130);
Monster[] mArray = {m1,m2,m3,m4};
showMonstersInfo(mArray);
int round = 1;
do {
System.out.println("========第" + round + "回合========");
int random = (int) (Math.random() * 10 + 1);
Monster m = selectOne(mArray);
if (random <= 7) {
useNomalAttack(u, m);
}
else if(random <= 9) {
if (u.getMp() >= 15) {
System.out.println("奥特曼使用了魔法攻击");
u.magicalAttack(mArray);
for (Monster monster : mArray) {
if (monster.getHp() > 0) {
monster.against(u);
}
}
}
useNomalAttack(u, m);
}
else{
if(u.getMp() >= 40){
System.out.println("奥特曼使用了究极必杀技");
u.hugeAttack(m);
if (m.getHp() > 0) {
m.against(u);
}
}
else {
useNomalAttack(u, m);
}
}
if (u.getHp() > 0) {
u.resumeMp();
}
System.out.println(u.info());
showMonstersInfo(mArray);
round += 1;
} while (u.getHp() > 0 && !isAllDead(mArray));
if (u.getHp() > 0) {
System.out.println(u.getName() + "奥特曼胜利");
}
else {
System.out.println("小怪兽胜利");
}
}
private static void useNomalAttack(Ultraman u, Monster m) {//使用普通攻击
System.out.println("奥特曼使用了普通攻击");
u.attack(m);
if (m.getHp() > 0) {
m.against(u);
}
}
}
2. 手机类
public class MobilePhone {
private String brand;//品牌
private double size;//大小
private String owner;//机主
private double price;//价格
private boolean isSmart;//是否是智能机
private String[] installedApps;
private int numberOfApps;
public MobilePhone(String brand, double size, double price, boolean isSmart) {
this.brand = brand;
this.size = size;
this.price = price;
this.isSmart = isSmart;
if (isSmart) {
installedApps = new String[100];
}
}
//修改器 - 属性的setter方法
public void setOwner(String owner){
this.owner = owner;
}
//访问器 - 属性的getter方法
public String getOwner(){
return owner;
}
public void show(){
System.out.println(owner + "的价值" + price + "元的" + size + "英寸的" + brand + "手机");
}
public void call(String tel){
System.out.println(owner + "正在呼叫" + tel + "...");
}
public void sendShortMessage(String[] tels, String content){
for (String tel : tels) {
System.out.println("向" + tel + "发送信息: " + content);
}
}
public void installApp(String appName){
if (isSmart && numberOfApps < installedApps.length) {
System.out.println("正在安装" + appName);
installedApps[numberOfApps] = appName;
numberOfApps += 1;
}
else {
System.out.println("不能安装应用程序.");
}
}
public void uninstallApp(String appName){
if (isSmart) {
for (int i = 0; i < numberOfApps; i++) {
if (installedApps[i].equals(appName)) {
System.out.println("正在卸载" + appName);
for (int j = i; j < numberOfApps - 1; j++) {
installedApps[j] = installedApps[j + 1];
}
numberOfApps -= 1;
return;
}
}
}
else{
System.out.println("不能卸载应用");
}
}
public void runApp(String appName){
for(int i = 0; i < numberOfApps; i++){
if (installedApps[i].equals(appName)) {
System.out.println("启动应用" + appName);
return ;
}
}
}
}
3. 矩形类
public class RecTangle {
private double length;
private double width;
public RecTangle(double length, double width){
this.length = length;
this.width = width;
}
public double area(){
return length * width;
}
public double perimeter(){
return (length + width) * 2;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
public class RecTangleTest {
public static void main(String[] args) {
RecTangle recTangle = new RecTangle(5.5, 4.5);
System.out.println("周长" + recTangle.perimeter());
System.out.println("面积" + recTangle.area());
recTangle.setWidth(5.5);
System.out.println("周长" + recTangle.perimeter());
System.out.println("面积" + recTangle.area());
}
}
5. 定义坐标类,可以算坐标之间的距离
- (/** /)文档注释:public方法都需要注释
/**
* 平面上的点
* @author Apple
* @since 0.1
*/
public class Point {
private double x; //横坐标
private double y; //纵坐标
/**
* 构造器
* @param x 横坐标
* @param y 纵坐标
*/
public Point (double x,double y) {
// this.x = x;
// this.y = y;
this.moveTo(x, y);
}
/**
* 移动到
* @param newX 新的横坐标
* @param newY 新的纵坐标
*/
public void moveTo(double newX,double newY){//移动到
x = newX;
y = newY;
}
/**
* 移动了
* @param dx 横坐标的增量
* @param dy 纵坐标的增量
*/
public void moveBy(double dx,double dy){//移动了
x += dx;
y += dy;
}
/**
* 计算到另一个点的距离
* @param other 另一个点
* @return 两个点的距离
*/
public double distanceTo(Point other){
//this到other的距离
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
@Override
public String toString() {//可以将对象变成一个表达式
return "(" + x + "," + y + ")";
}
}
public class PointTest {
public static void main(String[] args) {
Point p1 = new Point(3.1, 5.2);
System.out.println(p1);//不用谢p1.toString。会自动调用
Point p2 = new Point(-2.3,-3.5);
System.out.println(p2);
System.out.println(p1.distanceTo(p2));
p1.moveTo(1, 1);
p2.moveTo(2, 2);
System.out.println(p1.distanceTo(p2));
}
}
6. 定义线段类,计算线段长度
- 此类里面直接调用了Point类,可直接表示一个坐标
/**
* 平面上的线条
* @author apple
*
*/
public class Line {
private Point start;
private Point end;
/**
* 构造器
* @param start 线段的起点
* @param end 线段的重点
*/
public Line(Point start, Point end){
this.start = start;
this.end = end;
}
/**
* 获取线段的长度
* @return 线段的长度
*/
public double length(){
return start.distanceTo(end);
}
}
public class LineTest {
public static void main(String[] args) {
Point p1 = new Point(3.1, 5.2);
Point p2 = new Point(-2.3,-3.5);
Line line = new Line(p1, p2);
System.out.println(line.length());
}
}