简体   繁体   English

如何创建子类以使其属性值不被父类覆盖?

[英]How to create subclass such that it's attribute values are not overridden from parent class?

*----------------*                 *----------------*         *---
|       Duck     |                 |   RubberDuck   |         |    
|----------------|                 |----------------|         |---
|color: ""       |   extends to    |color: "Yellow" |         |
|numLeg: 2       |   ---------->   |numLeg: 0       |   and   |     ...
|haveWings: True |                 |haveWings: True |         |
|----------------|                 |----------------|         |---
|swim()          |                 |swim()          |         |
*----------------*                 *----------------*         *---

From diagram above, If i want to extend Duck class to another Duck-like class with some different class attributes value. 从上面的图中,如果我想将Duck类扩展到具有一些不同类属性值的另一个Duck-like类。 How should i do? 我应该怎么做?

As i already searched some information i knew that attribute could not be overrided. 当我已经搜索了一些信息时,我知道该属性不能被覆盖。 then i think my design was wrong (wrong way in Object-Oriented design). 然后我认为我的设计是错误的(面向对象设计中的方法错误)。 Please give me some recommendation to improve my program (or re-design my program) in the "simple Object-Oriented way". 请给我一些建议,以“简单的面向对象的方式”改进程序(或重新设计程序)。

note that i don't care so much about Duck class (because i just use it as prototype). 请注意,我不太在意Duck类(因为我只是将其用作原型)。 Duck class will have attributes or doesn't have attributes that's not matter but in my design i just think that mother class should have some default values (that may be my misleading here). Duck类将具有属性或不具有无关紧要的属性,但是在我的设计中,我只是认为母类应具有一些默认值(这可能在这里引起我的误解)。

I don't see any prob here :) 我在这里看不到任何问题:)
Attributes value can be easily change, it's the attributes type which can't be modified. 属性值可以轻松更改,这是无法修改的属性类型。

public class Duck() {
  private String color;
  private int numLeg ;
  private boolean haveWings ;

  public Duck(String aColor, int aNumLeg, boolean hasWings) {
    color=aColor;
    numLeg=aNumLeg;
    haveWings=hasWings;
  }

  public Duck() {
    this("", 2, true); // a little trick for lazy man ;)
  }
}

then, you can easily change the attribute in a subclass, calling the Duck constructor. 然后,您可以轻松地在子类中更改属性,调用Duck构造函数。

public class SubDuck extends Duck {
  public SubDuck() {
    super("Yellow", 0, true);
  }
}

Create in Duck two constructors 在Duck中创建两个构造函数

  1. First one with no arguments, here you define your default values of attributes 第一个不带参数的参数,在这里定义属性的默认值
  2. Second one taking all values -> this one is to be used in subclasses 第二个取所有值->这个将在子类中使用

Use the overloaded constuctor to set parameters. 使用重载的构造器来设置参数。

//This constructor can only be called by inheriting classes 
//Since it is java, other classes in the package can do as well, but you can avoid. 
protected Duck(String color1, int numLeg1, int haveWings1){
    this.color = color1;
    this.numLeg = numLeg1;
    this.haveWings = haveWings1;   
}

//For setting default values and creating duck objects. 
//Not needed if you don't want to generate    plain Ducks
public Duck(){
    this.color = "";
    this.numLeg = 2;
    this.haveWings = true;
}

Then the RubberDuck Constructor where you call the duck-constructor to set the parameters. 然后,在RubberDuck构造函数中调用鸭子构造函数以设置参数。 public RubberDuck(){ super("yellow", 0, true); public RubberDuck(){super(“ yellow”,0,true); } And additional classes can use similar way: public PoorDuck(){ super("pink-blue", 1, false); }其他类可以使用类似的方式:public PoorDuck(){super(“ pink-blue”,1,false); } }

class Duck
{
 protected String color;
 protected int numLeg;
 protected boolean haveWings;

 public Duck()
 {
  color = "";
  numLeg = 2;
  haveWings = true;
 }

 public void swim()
 {
   //Do something
 }
}


class RubberDuck extends Duck
{
 public RubberDuck()
 {
  color = "yellow";
  numLeg = 0;
  haveWings = true;
 }

 public void swim()
 {
   //Do something
 }
}

如果您不关心并使用Duck作为原型,那么我建议创建Duck作为interfase或抽象类,然后您可以编写分别实现或扩展Duck的具体类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM