【问题标题】:Syntax on printing objects? [closed]打印对象的语法? [关闭]
【发布时间】:2016-05-18 22:21:22
【问题描述】:

路线:http://imgur.com/kw6A0JX

我认为我没有正确打印出对象。我的老师帮助我完成了第一部分,所以我相信我分配正确。打印出来时,我是否使用“this”命令?这种情况的正确语法是什么?

谢谢。

public static void main(String [ ] args) {

 Dog1 Rover = new Dog1("Rover", 4);
 Sheep1 Wooly = new Sheep1("Wooly", 4);
 Duck1 Daffy = new Duck1("Daffy", 2);
 Cat1 Ketty = new Cat1("Ketty", 4);

 System.out.println(name.Dog1, getHello.Dog1, isCarnivorous.Dog1, isMammal.Dog1);
 System.out.println(name.Sheep1, getHello.Sheep1, isCarnivorous.Sheep1, isMammal.Sheep1);
 System.out.println(name.Duck1, getHello.Duck1, isCarnivorous.Duck1, isMammal.Duck1);
 System.out.println(name.Cat11, getHello.Cat1, isCarnivorous.Cat1, isMammal.Cat1);

}

更新:

public abstract class Animal1 { //creating Animal1 which is the base and parent class, it is abstract so abstract classes can be created below

    private String animalName; //defining animalName as private 
    public int numberOfLegs; //# of legs as public 

public Animal1(final String name){ //first constructor with only assigning name 

        animalName = name;
} 

public Animal1(final String name, final int legs){ //second constructor assigning both name and number of legs

        animalName = name;
        numberOfLegs = legs;
}


public String getName(){ //first getMethod for animalName
        return animalName;
}


public int getLegs(){ //second getMethod for returning numberOfLegs
        return numberOfLegs;
}


public boolean isMammal(){ //returning true value with boolean
        return true;
}


public boolean isCarnivorous(){ //returning true value with boolean
        return true;
}


public abstract String getHello(); //creating an abstract method, possible because base class is also abstract

}


public class Cat1 extends Animal1{ //child class of Animal1

    public Cat1(final String name){ //Creating class constructor taking a name, within the constructor call the parent class constructor taking one argument
        super(name, 4); 
 }

    @Override
    public String getHello(){  //Overriding getHello to return "Meow" 
        return "Meow";
 }
}

public class Dog1 extends Animal1{ //another child of Dog1

    public Dog1(final String name){ //Creating class constructor taking a name, within the constructor call the parent class constructor taking one argument
        super(name, 4);
     }

    @Override
    public String getHello(){ //Overriding getHello to return "Woof"
        return "Woof";
     }
}

public class Duck1 extends Animal1{ //third child class of Animal1

    public Duck1(final String name){ //Creating class constructor taking a name, within the constructor call the parent class constructor taking one argument
        super(name, 2);
     }

    @Override
    public boolean isMammal(){ //Overriding isMammal() function to return false, as a duck is not a mammal 
        return false;
     }

    @Override
    public boolean isCarnivorous(){ //Overriding isCarnivorous() function to return false as a duck is not a carnivore
        return false;
     }

    @Override
    public String getHello(){ //Overriding getHello to return "Quack"
        return "Quack";
     }

}

public class Sheep1 extends Animal1{ //fourth child class of Animal1

    public Duck1(final String name){ //Creating class constructor taking a name, within the constructor call the parent class constructor taking one argument
        super(name, 2);
     }

    @Override
    public boolean isCarnivorous(){ //Overriding isCarnivorous() function to return false as a sheep is not a carnivore
        return false;
     }

    @Override
    public String getHello(){ //Overriding getHello to return "Baa"
        return "Baa";
     }

}

public static void main(String [ ] args) {

 Dog1 Rover = new Dog1("Rover", 4);
 Sheep1 Wooly = new Sheep1("Wooly", 4);
 Duck1 Daffy = new Duck1("Daffy", 2);
 Cat1 Ketty = new Cat1("Ketty", 4);

System.out.println(Rover.getName() + ", " + Rover.getHello() + ", " + Rover.isCarnivorous() + ", " + Rover.isMammal());
System.out.println(Wooly.getName() + ", " + Wooly.getHello() + ", " + Wooly.isCarnivorous() + ", " + Wooly.isMammal());
System.out.println(Daffy.getName() + ", " + Daffy.getHello() + ", " + Daffy.isCarnivorous() + ", " + Daffy.isMammal());
System.out.println(Ketty.getName() + ", " + Ketty.getHello() + ", " + Ketty.isCarnivorous() + ", " + Ketty.isMammal());

}

【问题讨论】:

  • 发布您的Dog1 课程。
  • “我认为我没有正确打印出对象。” - 你当然知道! 运行它。它有效吗?你做对了!它不工作吗?问问自己,“为什么?”
  • 请不要破坏您的帖子。
  • @JohnDoe9292 你宁愿不喜欢它,但是人们仍然可以进入编辑历史并查看你的PROJECTS
  • @JohnDoe9292 这不是它的工作原理。 SO 不是支持台。它是收集知识的宝库。一旦您发布了一个问题,它就会永久存在,以帮助将来可能遇到类似问题的其他人。如果您的问题已经解决,请将解决问题的答案标记为正确。

标签: java syntax


【解决方案1】:

您的语法错误。您需要按名称引用变量,而不是按类。方法在对象之后。而System.out.println() 不接受多个参数。试试这个:

System.out.println(Rover.getName() + ", " + Rover.getHello() + ", " + Rover.isCarnivorous() + ", " + Rover.isMammal());

其他行也是如此。

【讨论】:

  • getHello 很可能是一种方法,所以应该是getHello()
  • @Blorgbeard 你可能是对的,但问题并不清楚,name 听起来更像是一个字段。
  • 抱歉,我是新手。我回头一看,都是方法,甚至是名字。应该是 getName。
  • @JohnDoe9292 更新了我的答案。
【解决方案2】:

你的语法颠倒了。如果这些都是您正在调用的方法,那么它们就像Dog1.name() 一样完成。 如果它们只是公共变量,您可以将它们称为Dog1.name

另外,一个忠告 - java 中的大多数对象瞬间都遵循第一个单词小写的语法,然后是大写单词(就像你的方法一样)。不重要,但有助于了解。

编辑:是的,这就是这个答案的第一行。要从您的动物类中获取布尔值,只需先使用对象名称调用它们,然后使用 .exampleMethod()

另外,对于您的打印语句,如果您保持原样,println 方法可能会打印出有趣的语句。你可以做的只是在两者之间添加一些字符串,如下所示:

System.out.println("Name: " + Dog1.getName() + ", Hello: " + Dog1.getHello()...); // rest of line excluded for brevity

【讨论】:

  • 我用整个程序更新了主帖。你能看看吗?谢谢
  • 感谢您帮助我。真的很感激。
【解决方案3】:

关键字this指的是当前对象。

public class Test
{
    public static void main(String[] args)
    {
        Dog1 dog = new Dog1("Rover", 4);

        System.out.println(dog.name() + " " + dog.hello() + " " + dog.carnivorus() + " " + dog.mammal() + "."); 
    }

}

class Dog1
{
    private String name;
    private int age;
    private String hello;
    private boolean carnivorus;
    private boolean mammal;

    public Dog1(String name, int age)
    {
        this.name = name;
        this.age = age;
        this.hello = "woof woof";
        this.carnivorus = true;
        this.mammal = true;
    }

    public String name()
    {
        return this.name;
    }

    public String hello()
    {
        return this.hello;
    }

    public boolean carnivorus()
    {
        return this.carnivorus;
    }

    public boolean mammal()
    {
        return this.mammal;
    }
}

【讨论】:

  • 我用整个程序更新了帖子。它们看起来非常相似,但没有这个。我做对了吗?
猜你喜欢
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多