Java中的抽象? [英] Abstraction in Java?
问题描述
今天我从朋友那里听到,封装不仅实现了信息隐藏,还实现了抽象。它是如何实现的?
Today i heard from my friend, that encapsulation is not only achieving information hiding but also abstraction. How does it achieve?
public class employee {
private String name;
private int id;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
上面的例子实现了我允许上课的封装访问我的公共方法而不是私有成员,但这里的抽象在哪里?任何人都可以用一种清晰的方式向我解释抽象。
The above example achieves encapsulation where i am allowing the class to access my public method rather than private members, but where does the abstraction come into picture here? Can anyone explain me on abstraction in a bit clear manner.
推荐答案
有两种不同的东西,信息隐藏和抽象。
There's two different things, information hiding and abstraction.
信息隐藏使抽象成为可能,但它有所不同。例如,使用您的代码
Information hiding makes abstraction possible, but it is something different. For example, using your code
public class employee {
private String name;
private int id;
public void setName(String name) {
this.name = name;
}
public String getName(){
return name;
}
}
id
字段实际上是隐藏的。这允许人们以与程序的其余部分分离的方式处理id。您的名称字段实际上也是隐藏的,因为您不直接访问名称字段,但 getName
和 setName $ c $中的代码c>确实。
The id
field is actually hidden. This allows one to handle ids in a manner that is decoupled from the rest of the program. Your name field is actually hidden too, as you don't access the name field directly, but the code in getName
and setName
does.
一旦从其余代码中隐藏数据结构,强制通过方法访问,就可以创建许多可替换的实现一个物品。例如,员工
是概念性的人
,因此您可以像这样重写上述内容:
Once you hide the structure of the data from the rest of the code, forcing access through methods, it is possible to create a number of replaceable implementations of an item. For example, an employee
is a conceptual kind of person
, so you could rewrite the above like so:
public interface Person {
public abstract String getName();
}
public class Employee implements Person {
private String name;
private int id;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
现在你的代码可以处理员工
作为人
。重写未明确处理 Employee
的其余代码以处理 Person
s后,你可以实现其他类型的 Person
,并利用现在 Person
任务的非员工特定任务。
Now your code can deal with the Employee
as a Person
. After rewriting the rest of the code that doesn't explicitly deal with Employee
s to deal with Person
s, you could implement other kinds of Person
s and leverage the non-Employee specific tasks that are now Person
tasks.
public Customer implements Person {
private String name;
private integer moneySpent;
public String getName() {
return name;
}
}
所以一个人搜索例行程序,只要它只索引人
对象现在可以包括员工
和客户$ c $的搜索C>秒。这是因为处理
Person
对象的代码实际上处理的是更高级别的抽象, Employee
和客户
对象共享。
So a person searching routine, as long as it only indexes Person
objects can now include searches of both Employee
s and Customer
s. This is because the code dealing with Person
objects is actually dealing with a higher level abstraction that both Employee
and Customer
objects share.
在抽象级别处理对象时,方法的名称在抽象中共享;但是,执行的实际代码取决于对象的未提及的基础类型。换句话说,如果你问一个人(恰好是一名雇员) getName()
那么它将以 Employee.getName()回应
函数,而 Customer
将以 Customer.getName()
函数响应。由于调用 getName()
的代码在 Person
上运行,因此它不知道它将处理哪种类型的人,但行为的明显变化(基于每个对象选择正确的代码块)仍然会发生。这种现象被称为Polymorphisim,如果你第一次触及这些概念,你会听到Polymorphisim作为一个经常使用的词。
When dealing with Objects on an abstract level, the names of the methods are shared across the abstraction; but, the actual code executed depends on the unmentioned underlying type of the object. In other words, if you ask a Person (who happens to be an employee) getName()
then it will respond with the Employee.getName()
function, while a Customer
will respond with a Customer.getName()
function. Since the code calling getName()
is operating on Person
s it has no idea which type of person it will be handling, but the apparent change in behavior (the selection of the right block of code on a per-object basis) still happens. This phenomena is known as Polymorphisim, and if you are first hitting these concepts, you'll hear Polymorphisim as a word used a lot.
多态行为的一个例子: / p>
An example of polymorpic behavior:
public interface Animal {
public abstract String makeSound();
}
public class Cow implements Animal {
public String makeSound() {
return "Moo Moo!";
}
}
public class Dog implements Animal {
public String makeSound() {
return "Ruff Ruff!";
}
}
public class Sheep implements Animal {
public String makeSound() {
return "Baa Baa!";
}
}
// this class demonstrates the polymorphic behavior
public class Farm {
public static void main(String[] args) {
ArrayList<Animal> animals = new ArrayList<Animal>();
animals.add(new Cow());
animals.add(new Sheep());
animals.add(new Dog());
for (Animal animal : animals) {
// this is where the polymorphisim occurs
// each animal will make a different sound
// because the makeSound method is getting
// bound to different blocks of code based
// on the exact type of animal class hiding
// under the Animal abstraction.
System.out.println(animal.makeSound());
}
}
}
预期输出:
Moo Moo!
Baa Baa!
Ruff Ruff!
即使我们从未明确更改类,也从未明确更改过方法。它是抽象方法与正在改变的显式子类的绑定,这种情况只发生在支持多态性的系统中。
even though we never explicitly changed classes, and we never explicitly changed methods. It was the binding of the abstract method to the explicit subclass that was changing, which is something that only happens in systems that support polymorphisim.
这篇关于Java中的抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!