public加类名(double weight)是什么意思
时间: 2024-10-08 21:10:03 浏览: 44
`public class 名称 (double weight)` 这个语法是在 Java 中定义一个公共访问权限的类,并且包含一个名为 `weight` 的实例字段,其数据类型为 `double`。
- `public` 是修饰符,表示这个类可以从任何地方被访问,包括其他包中的类。
- `class 名称` 定义了类的名字,这是标识符的一部分,用于在整个程序中唯一地引用这个类。
- `(double weight)` 是构造函数的参数列表,其中 `weight` 是一个带有默认值的实例变量。这意味着当你创建这个类的新对象时,如果没有提供自定义的 `weight` 值,默认情况下它的初始值将是 `0.0`(因为 `double` 类型没有默认值)。
例如:
```java
public class Person {
// 公开的Person类,带有重量(double)属性的构造函数
public double weight; // 实例变量
// 构造函数,接收一个double类型的重量参数
public Person(double weight) {
this.weight = weight;
}
}
```
相关问题
用java编写一个人“类”的声明,类名是person,描述人的成员变量有height、weight、sex、name、age等,还有房产house的声明
在Java中,你可以这样声明一个名为Person的人类类,它包含成员变量如身高(height)、体重(weight)、性别(sex)、姓名(name)和年龄(age),以及一个表示房产的House对象:
```java
// 定义一个Person类
public class Person {
// 成员变量
private double height; // 身高(单位:米)
private double weight; // 体重(单位:千克)
private String sex; // 性别
private String name; // 姓名
private int age; // 年龄
// 房产类的引用
private House house;
// 构造函数
public Person(double height, double weight, String sex, String name, int age, House house) {
this.height = height;
this.weight = weight;
this.sex = sex;
this.name = name;
this.age = age;
this.house = house;
}
// getter和setter方法,用于访问和修改成员变量
public double getHeight() { return height; }
public void setHeight(double height) { this.height = height; }
// 其他getter和setter方法... (类似地为weight, sex, name, age, 和house)
// 如果House是一个独立的类,声明如下:
class House {
// 属性和方法...
}
}
```
设计一个汽车类 Car 类,包含的属性有车轮的个数 wheels 和车重 weight。 小汽车类 SmallCar 是 Car 类的子类。包含的属性有载人数 loader。卡车 类 Truck 类是 SmallCar 类的子类,其中包含的属性有载重量 payload,每 个类都有构造方法和输出相关数据的方法。编写程序测试类。java编写在一个类名
为 CarDemo 的文件中。
以下是代码实现:
```java
public class Car {
protected int wheels;
protected double weight;
public Car(int wheels, double weight) {
this.wheels = wheels;
this.weight = weight;
}
public void display() {
System.out.println("车轮个数:" + wheels);
System.out.println("车重:" + weight);
}
}
public class SmallCar extends Car {
private int loader;
public SmallCar(int wheels, double weight, int loader) {
super(wheels, weight);
this.loader = loader;
}
public void display() {
super.display();
System.out.println("载人数:" + loader);
}
}
public class Truck extends SmallCar {
private double payload;
public Truck(int wheels, double weight, int loader, double payload) {
super(wheels, weight, loader);
this.payload = payload;
}
public void display() {
super.display();
System.out.println("载重量:" + payload);
}
}
public class CarDemo {
public static void main(String[] args) {
Car car = new Car(4, 1000);
SmallCar smallCar = new SmallCar(4, 800, 5);
Truck truck = new Truck(6, 5000, 3, 10000);
System.out.println("普通汽车:");
car.display();
System.out.println();
System.out.println("小汽车:");
smallCar.display();
System.out.println();
System.out.println("卡车:");
truck.display();
System.out.println();
}
}
```
以上代码实现了一个基本的汽车类和两个子类 SmallCar 和 Truck。在测试类 CarDemo 中,分别创建了一个普通汽车、一个小汽车和一个卡车,然后通过调用对应的 display() 方法输出各自的信息。
注意:为了让子类能够访问父类的属性和方法,需要使用关键字 `super`。同时,为了让子类能够覆盖父类的方法,需要在子类中重新定义同名的方法。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)