English 中文(简体)
提出反对时的预测
原标题:Upcasting when making object

Say you have a Shape base class and various derived types: Circle, etc.

没有任何理由在提出新目标时,以书面形式提出:

Shape s = new Circle();

而不是:

Circle s = new Circle();

这两种发言中的每一份都以不同的方式提出的<条码>物体?

最佳回答

这两项声明将产生与核查和核查机制相同的目标。 表明你只是计划将物体用于基级或接口,这并非罕见。 例如,这很常见:

List<String> list = new ArrayList<String>();

虽然一般不是好主意,但你可以投下<条码>。 缩略语 如果您知道,它是一个,例如<代码>。 缩略语

if (s instanceof Circle) {
    Circle c = (Circle) s;
    // handle Circle case
}
问题回答

你可以辩称,你的第一个例子(即<代码>)。 Shape s = 新的圆环;)与“连接接口”具有同等优势,即使Shape可能是抽象的,甚至可能是具体的基类。 例如,如果你只使用Shape确定的方法,而不是“圆环”确定的方法,那么你就能够非常容易地把你使用的方法改变到广场,例如通过修改一条法典,即Shape s = 新的广场()。

您的两个例子中,目标都是一样的,因此可以认为第一种选择是更好的。 连接接口能够使编码基础更加容易挖掘和易变。

立即发射的物体完全相同。

预测的后果是:

  1. generalization while storing : all objects of different specialized type can be threated as the same interface and can be stored together into data structure. (Like pointed out by @WhiteFang34 : List < Shape>)
  2. generalization while accessing objects method: the client class can call Shape s method without knowing the real type (typically you can use a factory to create object that returns the common interface Shape, so the client class is unaware of the real type of the objects but only the Shape s interface methods are exposed)

在回答你的问题时,这两个目标完全相同。 这两种提法都可以指同一目标:

Circle c = new Circle();
Shape s = c;

连接接口使你能够改变执行类别,对你的代码产生最小的影响。 阁下:

Set<String> names = new HashSet<String>();         // using Set reference not HashSet
names.add("Frank");
names.add("John");
names.add("Larry");

for(String name: names) {
    System.out.println(name + " is in the team");
}

现在,你们的要求发生了变化,你希望以字母顺序印制姓名,即使用Hassheet而不是树Set。 由于您已登上接口,没有使用哈希特语类特定方法,你只得改变一条线:

Set<String> names = new TreeSet<String>();   // the only line of code that changes
names.add("Frank");
names.add("John");
names.add("Larry");

for(String name: names) {
    System.out.println(name + " is in the team");
}

接口还允许你使用依赖注射。 它允许你在撰写你的法典时使用甚至不存在的班级。

http://en.wikipedia.org/wiki/Dependency_injection”rel=“nofollow”>http://en.wikipedia.org/wiki/Dependency_injection

无,物体本身并不不同。 只有汇编者认为他所看到的情况不同(例如,有何种方法)。

这样做的一个原因就是表明你如何对待这个物体(因此,你可以轻易插入一个不同的衍生类别)。

Circle s = new Circle();
here object is refering to class Cricle

Shape s = new Circle(); here object it refering to interface

两者都是一样的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签