JAVA继承类phone_【Java基础】类-----继承

一、基本知识

1、在Java语言中,实现类的继承使用extends关键字

格式如下:

[public] class SubClass extends SuperClass{

//类体的定义

}

2、被继承的类称为父类或超类或基类,而继承得到的类称为子类或派生类。

3、子类继承父类的状态(属性)和行为(方法),同时也可以有自己的特征。

二、关于继承的注意事项

1、子类继承父类中非private的成员变量和成员方法,同时,注意构造方法不能被子类继承。

2、定义类时若缺省extends关键字,则所定义的类为java.lang.Object类的直接子类。

在Java语言中,一切类都是Object类的直接或间接子类。

3、Java仅支持单重继承,即一个类至多只有一个直接父类。在Java中通过接口来实现多重继承。

4、父类中定义的private成员变量和方法不能被子类继承,因此在子类中不能直接使用。我们可以在父类中定义公共的访问方法和修改方法,子类可以通过这些方法来实现访问和修改。

成员变量的访问:

直接通过子类对象访问成员变量:

等号左边是谁,就优先用谁,没有则向上找。

间接通过成员方法访问成员变量:

该方法属于谁,就优先用谁,没有则向上找。

规则:

局部变量: 直接写成员变量名

本类的成员变量: this.成员变量名

父类的成员变量: super.成员变量名

成员方法的访问

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: class Phone { private String brand; private String model; private int price; public Phone(String brand, String model, int price) { this.brand = brand; this.model = model; this.price = price; } public void call(String number) { System.out.println("Calling " + number); } public void sendMessage(String message) { System.out.println("Sending message: " + message); } public String getBrand() { return brand; } public String getModel() { return model; } public int getPrice() { return price; } } class SmartPhone extends Phone { private String operatingSystem; public SmartPhone(String brand, String model, int price, String operatingSystem) { super(brand, model, price); this.operatingSystem = operatingSystem; } public void browseInternet() { System.out.println("Browsing the internet"); } public String getOperatingSystem() { return operatingSystem; } } // Example usage SmartPhone myPhone = new SmartPhone("Samsung", "Galaxy S21", 999, "Android"); System.out.println("My phone is a " + myPhone.getBrand() + " " + myPhone.getModel() + " running " + myPhone.getOperatingSystem()); myPhone.call("555-1234"); myPhone.sendMessage("Hello, world!"); myPhone.browseInternet(); ### 回答2: 以下是一个关于Java继承手机的代码示例: ```java // 手机类 class Phone { private String brand; private String color; public Phone(String brand, String color) { this.brand = brand; this.color = color; } public void makeCall(String phoneNumber) { System.out.println("拨打电话给 " + phoneNumber); } public void sendMessage(String phoneNumber, String message) { System.out.println("给 " + phoneNumber + " 发送短信:" + message); } } // 智能手机类继承自手机类 class Smartphone extends Phone { private String operatingSystem; public Smartphone(String brand, String color, String operatingSystem) { super(brand, color); this.operatingSystem = operatingSystem; } public void installApp(String appName) { System.out.println("安装应用程序:" + appName); } public void takePhoto() { System.out.println("拍照"); } } // 主程序 public class Main { public static void main(String[] args) { Phone phone = new Phone("Nokia", "黑色"); phone.makeCall("123456789"); Smartphone smartphone = new Smartphone("Apple", "白色", "iOS"); smartphone.makeCall("987654321"); smartphone.sendMessage("987654321", "你好!"); smartphone.installApp("微信"); smartphone.takePhoto(); } } ``` 上述代码中,我们定义了两个类:`Phone`(手机类)和`Smartphone`(智能手机类)。`Phone`类具有拨打电话和发送短信的功能,而`Smartphone`类继承了`Phone`类,并在此基础上增加了安装应用和拍照的功能。在主程序中,我们创建了一个普通手机对象和一个智能手机对象,并分别调用了它们的各种方法。 ### 回答3: 下面是一个关于Java继承手机的简单代码示例: ```java // 定义手机类 class Phone { // 成员变量 protected String brand; protected String model; protected int price; // 构造方法 public Phone(String brand, String model, int price) { this.brand = brand; this.model = model; this.price = price; } // 成员方法 public void call() { System.out.println("打电话"); } public void sendMessage() { System.out.println("发送短信"); } } // 定义智能手机类,继承自Phone类 class SmartPhone extends Phone { // 成员变量 private int camera; // 构造方法 public SmartPhone(String brand, String model, int price, int camera) { super(brand, model, price); this.camera = camera; } //成员方法 public void takePhoto() { System.out.println("拍照"); } } // 主类 public class Main { public static void main(String[] args) { // 创建手机对象 Phone phone = new Phone("华为", "Mate 20", 2999); phone.call(); phone.sendMessage(); // 创建智能手机对象 SmartPhone smartPhone = new SmartPhone("苹果", "iPhone X", 6999, 1200); smartPhone.call(); smartPhone.sendMessage(); smartPhone.takePhoto(); } } ``` 这段代码中,有两个类:Phone(手机)和SmartPhone(智能手机)。SmartPhone类继承自Phone类,SmartPhone具有Phone的属性和方法,并且还有自己的特殊属性和方法。在代码的主方法中,分别创建了Phone对象和SmartPhone对象,并调用它们的方法来模拟使用手机和智能手机的行为。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值