Java中的抽象? [英] Abstraction in Java?

查看:22
本文介绍了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.您的姓名字段实际上也是隐藏的,因为您不直接访问姓名字段,但是 getNamesetName 中的代码可以.

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.

一旦您对其余代码隐藏了数据结构,强制通过方法访问,就可以创建一个项目的多个可替换实现.例如,employee 是一种概念上的 person,因此您可以像这样重写上面的内容:

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 处理.在将没有显式处理Employees 的其余代码重写为处理Persons 之后,您可以实现其他类型的Persons 并利用现在是 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 Employees to deal with Persons, you could implement other kinds of Persons 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;
     }
}

所以一个人搜索例程,只要它只索引Person 对象,现在可以包括对Employees 和Customers 的搜索.这是因为处理 Person 对象的代码实际上是在处理 EmployeeCustomer 对象共享的更高级别的抽象.

So a person searching routine, as long as it only indexes Person objects can now include searches of both Employees and Customers. This is because the code dealing with Person objects is actually dealing with a higher level abstraction that both Employee and Customer objects share.

在抽象层处理对象时,方法的名称在抽象层之间共享;但是,实际执行的代码取决于未提及的对象底层类型.换句话说,如果你问一个 Person(碰巧是员工)getName() 那么它会用 Employee.getName() 函数响应,而 getName()code>Customer 将使用 Customer.getName() 函数进行响应.由于调用 getName() 的代码对 Person 进行操作,它不知道它将处理哪种类型的人,但行为的明显变化(选择每个对象的正确代码块)仍然发生.这种现象被称为多态,如果你第一次接触这些概念,你会听到多态是一个经常使用的词.

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 Persons 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.

多态行为的一个例子:

 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆