public int getWeight() { return weight; }
时间: 2024-03-28 21:36:24 浏览: 10
这段代码是Food类中的一个公有方法getWeight,该方法返回一个整型的weight属性值。
通过定义这个方法,可以让其他类访问Food对象的weight属性,而不需要将weight属性设置为public类型。这样可以保护weight属性,避免在其他类中对其进行非法修改。同样地,也可以定义其他类似的访问方法来获取或修改其他属性值,以提高代码的封装性和安全性。
相关问题
abstract class Express{ public int weight; public abstract int getWeight(); public abstract int getTotal(); } public class SLExpress extends Express{ public int cost; public SLExpress(){ } public SLExpress(int weight){ super.weight=weight; } public int getTotal(int weight){ cost=12+(weight-1)*2; return cost; } } public class DDExpress extends Express{ public int cost; public DDExpress(){ } public DDExpress(int weight){ super.weight=weight; } public int getTotal(int weight){ cost=5+weight-1; return cost; } } public class CaicaiStation{ public int sum; public int calculate(Express[] ex){ for(int i=0;i<ex.length;i++){ sum+=ex[i]; } } }纠错
在这段代码中,`CaicaiStation`类中的`calculate()`方法中有一个错误:
1. 在`sum+=ex[i];`语句中,`ex[i]`是`Express`类型的对象,不能直接进行加法操作,需要调用`ex[i].getTotal()`方法来获取该快递的总费用。
下面是修改后的代码:
```
abstract class Express {
public int weight;
public abstract int getWeight();
public abstract int getTotal();
}
public class SLExpress extends Express {
public int cost;
public SLExpress() {
}
public SLExpress(int weight) {
super.weight = weight;
}
public int getWeight() {
return weight;
}
public int getTotal() {
cost = 12 + (weight - 1) * 2;
return cost;
}
}
public class DDExpress extends Express {
public int cost;
public DDExpress() {
}
public DDExpress(int weight) {
super.weight = weight;
}
public int getWeight() {
return weight;
}
public int getTotal() {
cost = 5 + weight - 1;
return cost;
}
}
public class CaicaiStation {
public int sum;
public int calculate(Express[] ex) {
for (int i = 0; i < ex.length; i++) {
sum += ex[i].getTotal();
}
return sum;
}
}
```
续写代码public abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void makeSound(); } // 包 animal package animal; public class Cat extends Animal { private int age; public Cat(String name, int age) { super(name); this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void makeSound() { System.out.println("Meow"); } } public class Dog extends Animal { private String breed; public Dog(String name, String breed) { super(name); this.breed = breed; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } @Override public void makeSound() { System.out.println("Woof"); } } public class Elephant extends Animal { private int weight; public Elephant(String name, int weight) { super(name); this.weight = weight; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override要求0.必须有抽象父类,且被继承 1.必须包含 >=2 个包2.必须创建 >=5 个类(其中包含抽象父类)3.类中属性皆为私有属性,需定义读写方法 4.子类需要重写个别父类方法
续写代码:
// 包 animal
package animal;
public abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void makeSound();
}
public class Cat extends Animal {
private int age;
public Cat(String name, int age) {
super(name);
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void makeSound() {
System.out.println("Meow");
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class Elephant extends Animal {
private int weight;
public Elephant(String name, int weight) {
super(name);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
***
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)