简体   繁体   English

java对象,我用动物speedy = new dog()创建动物或狗吗? 为什么?

[英]java objects, do I create an animal or a dog with animal speedy = new dog(); and why?

I don't get what I'm creating actually... Usually you create an object with dog speedy = new dog(); 我没有得到我正在创造的东西......通常你用dog speedy = new dog();创建一个对象dog speedy = new dog(); you call the constructor dog() which creates a dog object and speedy is the name of the reference to it. 你调用构造函数dog()创建一个dog对象, speedy是它的引用名称。 but what if the first dog is named "animal" ( dog extends animal ) ? 但是,如果第一只狗被命名为“动物”( dog延伸animal )怎么办?

Your question isn't very clear, but if your Dog class is extending Animal, and you want to use the Dog methods/fields, you need to create a new Dog. 你的问题不是很清楚,但是如果你的Dog类正在扩展Animal,并且你想使用Dog方法/字段,你需要创建一个新的Dog。 You could create an Animal a = new Dog() but you still won't be able to access the Dog methods/fields (although it shouldn't throw an error) 您可以创建一个Animal a = new Dog(),但您仍然无法访问Dog方法/字段(尽管它不应该抛出错误)

Static vs dynamic type of an object 静态与动态类型的对象

Consider the following classes: 考虑以下类:

class Animal {
    void eat() { System.out.println("Nom nom"); }
}
class Dog extends Animal {
    void eat() { System.out.println("I want a bone"); }
    void bark() { System.out.println("WOOF"); }
}

Now consider the following code: 现在考虑以下代码:

Animal dog = new Dog();

Question: what is the type of the variable dog ? 问题:变量dog的类型是什么? The answer depends on whether you want to know the static type or the dynamic type. 答案取决于您是否想知道静态类型或动态类型。

Static type is the type the compiler sees, which is Animal in this case. 静态类型是编译器看到的类型,在这种情况下是Animal This is also the type of the variable. 这也是变量的类型。 This will define which methods are available. 这将定义哪些方法可用。

Dynamic type is the type that is defined at runtime, it is the "real" type of the variable, which defines its behaviour. 动态类型是在运行时定义的类型,它是变量的“真实”类型,它定义了它的行为。

In the above code you will not be able to call bark() on variable dog , because its static type is Animal . 在上面的代码中,您将无法在变量dog上调用bark() ,因为它的静态类型是Animal However, when you call eat() , it will return I want a bone because its behaviour is defined by its dynamic type which is Dog . 然而,当你调用eat()它将返回I want a bone ,因为其行为是由它的动态类型被定义Dog

In many cases you do not know the dynamic type of an object. 在许多情况下,您不知道对象的动态类型。 For example if you write a method that has a parameter of type List , the actual dynamic type will be known only when someone calls your method with a List implementation (such as ArrayList , LinkedList etc.). 例如,如果您编写的方法具有List类型的参数,则只有当某人使用List实现(例如ArrayListLinkedList等)调用您的方法时,才会知道实际的动态类型。

This depends on the way in which you would like to use your dog object. 这取决于您想要使用狗对象的方式。 Consider this example: 考虑这个例子:

class Animal {
    public void eat() {
        System.out.writeln("nam nam nam");
    }
    public void sleep() {
        System.out.writeln("zzzz zzzz");
    }
}
class Dog extends Animal {
    public void bark() {
        System.out.writeln("ruff ruff");
    }
}

If you want to treat your dog like the rest of the animals, ie as someone who can eat and sleep, you do this: 如果您想像对待其他动物一样对待您的狗,即作为可以吃饭和睡觉的人,您可以这样做:

Animal fifi = new Dog();
fifi.eat();   // Works fine
fifi.sleep(); // Works fine
fifi.bark();  // Compile error!

However, Fifi cannot bark. 但是,菲菲不能吠叫。 If you do this, on the other hand, 另一方面,如果你这样做,

Dog speedy = new Dog();
speedy.eat();
speedy.sleep();
speedy.bark();

your Dog would be able to bark, in addition to also being able to eat and sleep, like the rest of the animals. 除了能够像其他动物一样吃饭和睡觉之外,你的Dog还可以吠叫。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是列表<dog>列表的子类<animal> ? 为什么 Java generics 不是隐式多态的?</animal></dog> - Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? 将List <Animal>转换为List <Dog> - Casting List<Animal> to List<Dog> 狗是动物,但清单 <Dog> 没有列出 <Animal> 。 如何在泛型/多态函数中安全使用它? - Dog is Animal but list<Dog> is not list<Animal>. How to use it safely in a generic/polymorphic function? 是否可以在没有演员的情况下从 Animal 列表中获取 Dog 列表? - Is it possible to get a list of Dog from a list of Animal without a cast? 是否可以使用类型参数 List 覆盖继承的方法<animal>与列表<dog> ?</dog></animal> - Is it possible to override an inherited method with type parameter List<Animal> with List<Dog>? 编写一个 Dog 构造函数,它有一个参数,即名称,并调用超级构造函数并传递名称和动物类型“狗”。 不能通过类型 - Write a Dog constructor that has one argument, the name, and calls the super constructor passing it the name and the animal type “dog”. cant pass type 类型不匹配:无法从地图转换 <String,List<Animal> &gt;到地图 <String,List<Dog> &gt; - Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>> 用子字符串创建Animal对象 - Create Animal object with substrings 创建狗类和测试人员 - Create a dog class and tester 使用Java中的枚举用动物对象填充动物园对象 - Populating a zoo object with animal objects using enum in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM