手机类的定义及调用
- class Test2_Phone {
- public static void main(String[] args) {
- Phone p1 = new Phone();
- p1.setBrand("锤子");
- p1.setPrice(998);
- System.out.println(p1.getBrand() +"手机" + p1.getPrice());
- p1.call();
- p1.sendMessage();
- p1.playGame();
- }
- }
- class Phone {
- private String brand;
- private int price;
- //品牌设置及获取方法
- public void setBrand(String brand) {
- this.brand = brand;
- }
- public String getBrand() {
- return brand;
- }
- //价格设置及获取方法
- public void setPrice(int price) {
- this.price = price;
- }
- public int getPrice() {
- return price;
- }
- public void call() {
- System.out.println("打电话");
- }
- public void sendMessage() {
- System.out.println("发短信");
- }
- public void playGame() {
- System.out.println("玩游戏");
- }
- }
复制代码 |
|