难道没人能给我解决这个java问题

来源:百度知道 编辑:UC知道 时间:2024/07/05 01:27:11
import java.io.*;
class Cleanser {
private String s = "Cleanser";
public void append(String a) { s += a; }
public void dilute() { append(" dilute()"); }
public void apply() { append(" apply()"); }
public void scrub() { append(" scrub()"); }
public String toString() { return s; }
public static void main(String[] args) {
Cleanser x = new Cleanser();
x.dilute(); x.apply(); x.scrub();
print(x);
}
}

public class Detergent extends Cleanser {
// Change a method:
public void scrub() {
append(" Detergent.scrub()");
super.scrub(); // Call base-class version
}
// Add methods to the interface:
public void foam() { append(" foam()"); }
// Test the new class:
public static void main(String[] args) {
Detergent x = new Detergent();
x.dilute();
x.apply();
x.scrub();
x.foam(

仔细确认你的print是哪里的方法,首先你的这个类里没有定义这个方法,所以你不可以直接使用print();如果你有继承含有print()方法的类也可以使用,但你也没有,所以你的print()方法是一个没有定义的方法。请先在你的父类中定义,或者继承含有print方法的类,再或者按照new XXX().print();的方式来调用含有该方法定义的类的print()方法。

第一 你想在jar包中引入print这个方法吧 这个方法在哪定义的先弄清楚啊 然后把那个类再导进来
第二 public void dilute() { append(" dilute()"); }
public void apply() { append(" apply()"); }
public void scrub() { append(" scrub()"); }
这几个是有问题的,不会达到你想要的效果的,仔细看看吧

你调用的是谁的print方法?这个类里根本就没有定义这个方法,你怎么能用? 这可和c++的命名空间不一样,不是你导了个包就不用写类名的。你这样写,就意味着你的这个类中有print()方法,才可以用。
而且还有问题,应该把有public static void main()方法的类设为public class。

大哥啊,你都没重载print方法你就直接用,那能不报错嘛?
还是你没发全代码啊。还有我很奇怪,为什么一个java文件里写俩类第二个类居然是public属性? 我确定你没发全代码

1. 你的上面的a是什么?
2。第一个类中没有继承怎么去调用父类
(我是个新手要是回答不对请见谅)

全部换成System.out.print(x);