【免费Java系列】给大家出一些JavaSE基础第七天的内容案例 , 让大家更好的理解与掌握

 

 面向对象

好的,以下是一些Java面向对象基础的案例:

案例一

1. 定义一个学生类Student,包含姓名、年龄、性别等属性,以及学习、吃饭等行为。然后创建一个学生对象,调用其行为方法。

public class Student {
    private String name;
    private int age;
    private String gender;

    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public void study() {
        System.out.println(name + "正在学习");
    }

    public void eat() {
        System.out.println(name + "正在吃饭");
    }
}

public class Main {
    public static void main(String[] args) {
        Student stu = new Student("小明", 18, "男");
        stu.study();
        stu.eat();
    }
}

案例二

2. 定义一个汽车类Car,包含品牌、型号、颜色等属性,以及启动、加速等行为。然后创建一个汽车对象,调用其行为方法。

public class Car {
    private String brand;
    private String model;
    private String color;

    public Car(String brand, String model, String color) {
        this.brand = brand;
        this.model = model;
        this.color = color;
    }

    public void start() {
        System.out.println(brand + "汽车启动了");
    }

    public void accelerate() {
        System.out.println(brand + "汽车加速了");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("奥迪", "A4", "黑色");
        car.start();
        car.accelerate();
    }
}


案例三

3. 定义一个银行账户类BankAccount,包含账户名、余额等属性,以及存款、取款等行为。然后创建一个银行账户对象,调用其行为方法。

public class BankAccount {
    private String accountName;
    private double balance;

    public BankAccount(String accountName, double balance) {
        this.accountName = accountName;
        this.balance = balance;
    }

    public void deposit(double amount) {
        balance += amount;
        System.out.println("存入" + amount + "元成功,当前余额为:" + balance);
    }

    public void withdraw(double amount) {
        if (amount > balance) {
            System.out.println("余额不足,无法取款");
        } else {
            balance -= amount;
            System.out.println("取出" + amount + "元成功,当前余额为:" + balance);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount("张三", 10000);
        account.deposit(5000);
        account.withdraw(3000);
    }
}

案例四

假设我们要设计一个动物园管理系统,其中包含动物和游客两个类。动物类有名字、年龄、种类等属性,以及吃、睡觉等行为;游客类有姓名、年龄、性别等属性,以及参观、购票等行为。

首先,我们定义动物类Animal:

public class Animal {
    private String name;
    private int age;
    private String type;

    public Animal(String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
    }

    public void eat() {
        System.out.println(name + "正在吃东西");
    }

    public void sleep() {
        System.out.println(name + "正在睡觉");
    }
}

然后,我们定义游客类Visitor:

public class Visitor {
    private String name;
    private int age;
    private String gender;

    public Visitor(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public void visit() {
        System.out.println(name + "正在参观动物园");
    }

    public void buyTicket() {
        System.out.println(name + "正在购买门票");
    }
}

最后,我们可以在主函数中创建动物和游客对象,并调用它们的行为方法:

public class Main {
    public static void main(String[] args) {
        Animal lion = new Animal("狮子", 5, "哺乳动物");
        lion.eat();
        lion.sleep();

        Visitor tom = new Visitor("Tom", 20, "男");
        tom.visit();
        tom.buyTicket();
    }
}

 最后运行结果如下:

```
狮子正在吃东西
狮子正在睡觉
Tom正在参观动物园
Tom正在购买门票

``` 


今天辅助掌握的案例就到此为止啦 , 大家点赞收藏关注一下哦 , 后面持续更新课程 ! 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值