Day_12_07

面向对象思想

经典案例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());
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容