面向对象思想
经典案例1
package Day12_07_01;
import java.util.Scanner;
public class Text2
{
private static final double AISLE_UNIT_PRICE = 38.5;
private static final double F_UNIT_PRICE = 15.5;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("请输入圆的半径:");
double r=input.nextDouble();
Circle small =new Circle(r);
Circle big=new Circle(r+3);
System.out.printf("围墙的造价: ¥%.2f\n",big.perimeter()*F_UNIT_PRICE);
System.out.printf("过道的造价: ¥%.2f\n",(big.area()-small.area())*AISLE_UNIT_PRICE);
input.close();
}
}
package Day12_07_01;
public class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double perimeter(){
return 2*Math.PI*radius;
}
public double area(){
return Math.PI*radius*radius;
}
}
案例2
package Day12_07_01;
public class Ultraman
{
String name;
int hp;
int mp;
public int getHp()
{
return hp;
}
public void setHp(int hp)
{
this.hp = hp > 0 ? hp : 0;
}
public int getMp()
{
return mp;
}
public void setMp(int mp)
{
this.mp = mp;
}
// 创建构造器
public Ultraman(String name, int hp, int mp)
{
this.name = name;
this.hp = hp;
this.mp = mp;
}
public int attact(Monster monster)
{
int injury = (int) (Math.random() * 11 +20);
monster.setHp(monster.getHp() - injury);
return injury;
}
public void hugeAttact(Monster monster)
{
if (mp >= 40)
{
int injory = monster.getHp() / 4 * 3 > 50 ? monster.getHp() / 4 * 3 : 50;
monster.setHp(monster.getHp() - injory);
mp -= 40;
}
}
public void magicalAttact(Monster[] mArray)
{
if (mp >= 15)
{
for (Monster monster : mArray)
{
int injury = (int) (Math.random() * 11 + 20);
monster.setHp(monster.getHp() - injury);
}
mp -= 15;
}
}
public String info()
{ // %s放入一个字符串
return String.format("%s奥特曼-生命值:%d,魔法值:%d", name, hp, mp);
}
}
package Day12_07_01;
public class Monster
{
String name;
int hp;
public int getHp()
{
return hp;
}
public void setHp(int hp)
{
this.hp = hp < 0 ? 0 : hp;
}
public Monster(String name, int hp)
{
this.name = name;
this.hp = hp;
}
public int attact(Ultraman ultraman)
{
int injury = (int) (Math.random() * 11 + 10);
ultraman.setHp(ultraman.getHp() - injury);
return injury;
}
public String info()
{
return String.format("%s小怪兽-生命值:%d", name, hp);
}
}
package Day12_07_01;
//在main方法中可以直接调用的方法必须要加上static
public class Text3
{
//判断奥特曼攻击的攻击的哪支小怪兽
public static Monster selectOne(Monster[] mArray)
{
Monster temp = null;
do
{
int randomIndex = (int) (Math.random() * mArray.length);
temp = mArray[randomIndex];
} while (temp.getHp() == 0);
return temp;
}
//显示所有的小怪兽的信息
public static void shouMonsterInfo(Monster[] mArray)
{
for (Monster m : mArray)
{
System.out.println(m.info());
}
}
//判断小怪兽是否死光了
public static boolean isAllDead(Monster[] mArray)
{
for (Monster monster : mArray)
{
if (monster.getHp() > 0)
{
return false;
}
}
return true;
}
public static void monsterIsAttact(Ultraman ultraman, Monster monster)
{
if (monster.getHp() > 0)
{
monster.attact(ultraman);
}
}
public static void main(String[] args)
{
Ultraman ultraman = new Ultraman("舒玲", 200, 150);
System.out.println(ultraman.info());
Monster monster1 = new Monster("王焕琪", 100);
Monster monster2 = new Monster("王琪", 100);
Monster monster3 = new Monster("王焕", 100);
Monster monster4 = new Monster("焕琪", 100);
Monster[] mArray ={ monster1, monster2, monster3, monster4 };
shouMonsterInfo(mArray);
int round = 1;
do
{
System.out.println("======第" + round + "回合=======");
int random = (int) (Math.random() * 10 + 1);
Monster monster =selectOne(mArray);
if (random <= 7)
{
System.out.println("奥特曼使用了普通攻击!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
} else if (random <= 9)
{
if (ultraman.getMp() > 15)
{
System.out.println("奥特曼使用了魔法攻击!");
ultraman.magicalAttact(mArray);
for (Monster m : mArray)
{
monsterIsAttact(ultraman, m);
}
} else
{
System.out.println("奥特曼使用了普通攻击!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
}
} else
{
if (ultraman.getMp() > 50)
{
System.out.println("奥特曼使用了必杀技!");
ultraman.hugeAttact(monster);
} else
{
System.out.println("奥特曼使用了普通攻击!");
ultraman.attact(monster);
monsterIsAttact(ultraman, monster);
}
}
System.out.println(ultraman.info());
shouMonsterInfo(mArray);
round++;
} while (ultraman.getHp() > 0 &&!isAllDead(mArray));
if (ultraman.getHp() > 0)
{
System.out.println("奥特曼胜利!");
} else
{
System.out.println("小怪兽胜利!");
}
}
//小怪兽是否攻击奥特曼
}
注释文档的使用
在Line类中去调用Point类
package Day12_07_02;
/**
* 平面上的线条
* @author YY
*@since 0.1
*/
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.distance(end);
}
}
package Day12_07_02;
/**
* 平面上的点
* @author YY
* @since 0.1
*
*/
public class Point
{
//private类型不用写文档注释
private double x;
private double y;
/**
* 构造器
* @param x 横坐标
* @param y 纵坐标
*/
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return y;
}
public void setY(double y)
{
this.y = y;
}
public void show()
{
System.out.printf("横坐标:%.2f纵坐标:%.2f", x, y);
}
/**
* 移动到
* @param newX 新的横坐标
* @param newY 新的纵坐标
*/
public void moveTo(double newX, double newY)
{
x = newX;
y = newY;
}
public void moveBy(double dx, double dy)
{
x += dx;
y += dy;
}
/**
* 计算到另一个点的距离
* @param other 另一个点
* @return 两个点的距离
*/
public double distance(Point other)
{
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
public String toString()
{
return "(" + x + "," + y + ")";
}
}
package Day12_07_02;
public class Text4
{
public static void main(String[] args)
{
Point p1 = new Point(5, 5);
System.out.println(p1.toString());
Point p2 = new Point(2, 4);
System.out.println(p2.toString());
System.out.println(p1.distance(p2));
p1.moveBy(9, 9);
p2.moveTo(10, 10);
System.out.println(p1.distance(p2));
Line line=new Line(p1, p2);
System.out.println(line.length());
}
}