S
Shawn
Hi,
I have a modular design(code-reusage) question. In order to make my
point clear, I have made up the following fake situations:
Last year in a project, I have written a class Person:
class Person
{
private String sName;
private Char cGender; //M: male; F: female
private double dWeight;
private double dHeight;
...
public Person()
{
//some code
}
public String getName()
{
//some code
}
...
//more code
}
Now, I have a new project for the Zoo. I found that I need a class Animal:
class Animal
{
private String sName; //suppose each animal has a name too
private Char cGender; //M: male; F: female
private double dWeight;
private double dHeight;
...
public Animal()
{
//some code
}
public String getName()
{
//some code
}
...
//more code
}
What should I do? Suppose the Animal class has 90% code same as the
Person class. I can think of several answers:
(A) Using Person class as a guide, quickly write Animal class. Because
there is a guide, so writing Animal class won't take as much time as
writing Person class.
Problems: I feel there is no code-reusage at all, strictly speaking.
(B)Using abstract class: write an abstract class which captures the
common features of both Person class and Animal class. Then write the
subclasses of it, which are Person class and Animal class.
Problems: now I have to change the code (Person class) in last year's
project. I have finished and delivered the project already.
I am not sure there is a good way to solve such a problem. I just want
to know normally how experienced programmers handle it.
Thank you very much for all your feedback.
I have a modular design(code-reusage) question. In order to make my
point clear, I have made up the following fake situations:
Last year in a project, I have written a class Person:
class Person
{
private String sName;
private Char cGender; //M: male; F: female
private double dWeight;
private double dHeight;
...
public Person()
{
//some code
}
public String getName()
{
//some code
}
...
//more code
}
Now, I have a new project for the Zoo. I found that I need a class Animal:
class Animal
{
private String sName; //suppose each animal has a name too
private Char cGender; //M: male; F: female
private double dWeight;
private double dHeight;
...
public Animal()
{
//some code
}
public String getName()
{
//some code
}
...
//more code
}
What should I do? Suppose the Animal class has 90% code same as the
Person class. I can think of several answers:
(A) Using Person class as a guide, quickly write Animal class. Because
there is a guide, so writing Animal class won't take as much time as
writing Person class.
Problems: I feel there is no code-reusage at all, strictly speaking.
(B)Using abstract class: write an abstract class which captures the
common features of both Person class and Animal class. Then write the
subclasses of it, which are Person class and Animal class.
Problems: now I have to change the code (Person class) in last year's
project. I have finished and delivered the project already.
I am not sure there is a good way to solve such a problem. I just want
to know normally how experienced programmers handle it.
Thank you very much for all your feedback.