2013-02-23 75 views
2

我读了很多关于“接口”和“抽象类”的文章在面向对象的编程中,我们需要抽象吗?

基本上,我们在谈论对象的特性时使用“抽象类”。

而我们使用“接口”,当我们调查什么对象能够做。

但它仍然让我感到困惑,所以我为自己做了一个练习的例子。

所以现在我想到一个对象的货物;

public abstract class cargo { 

     protected int id; 

     public abstract int getWidth(int width); 

     public abstract int setWidth(int width); 

     public abstract int setHeight(int h); 

     public abstract int getHeight(int h); 

     public abstract int setDepth(int d); 

     public abstract int getDepth(int d); 

     public abstract int volume(int w,int h,int d); 

     public int getId(){ 
      return this.id; 
     } 

     public abstract int setId(); 

     public abstract void setBrand(); 

     public abstract void getBrand(); 

     .....so on , still have a lot of characteristic of a cargo 
    } 

    //in the other class 
    public class usaCargo extends cargo{ 
        .... 
        private 

    } 

因此,这里有几个关于我的设计的问题。

1.因此,在真正的编程项目世界中,我们是否真的像上面这样做?对我来说,我认为没关系设计,我们符合货物的基本特征。

  1. 如果我们设置“专用标识”,那么,我们却不能因为它是私人使用“身份证”在任何子类这个变量,所以是意味着我们在抽象类定义的每个变量必须是公共/保护?

  2. 有人可以举一些合适的例子,让我的货物可以实现一些界面吗?

    public interface registration{ 
        public void lastWarrantyCheck(); 
    } 
    

    ,但似乎不太适合在这里...

  3. 我们不经常内部接口定义变量,请我们?

我试着在OOP上获得更多的意义。原谅我很长的问题。

回答

1

你会在抽象类定义变量,这样的抽象类定义的方法有变量使用。这些变量的范围取决于您希望具体类如何访问这些变量:

private当您要强制某个具体类通过抽象类中定义的getter或setter时,应该使用这些变量。当你想给变量的具体类直接访问

protected应该被使用。

public当您希望变量可以被任何类访问时,应该使用它。

货物对象可能实施的合理界面可以设置为如何将货物从源移动到目的地。有些货物可能通过货运列车运输,有些可能可以通过飞机运输等等。实施可发货的具体类别,以及如何确定这种类型的货物将被运输。

public interface Shippable { 
    public void ship(); 
} 

最后,接口中定义的变量必须是公共静态的,最终意味着它是一个常量变量。

希望这能为你解决它!

1
  1. 抽象类可以包含实现,所以他们可以有私有变量和方法。另一方面,接口不能。

  2. 你可以找到一些关于如何实现接口here的例子。不过,我包括了你将如何实现你的注册示例如下。

    public class Cargo implements Registration{ 
        public void lastWarrantyCheck(){ 
         System.out.println("Last warranty check"); 
        }   
    } 
    
  3. 接口变量是可能的,但他们应该只包括常量声明(被声明为static和final的变量声明)。有关详情,请参阅here

1
  1. 变量在抽象类中可以声明为protected,他们只会在它和任何扩展类可用。 Private变量在扩展类中永远不可访问。

  2. 接口提供了实现它们的类所需的函数列表。例如,您可以使用接口hasWarranty来定义对象处理保修相关活动所需的所有功能。

    public interface hasWarranty { 
        public void lastWarrantyCheck(); 
        public void checkWarranty(); 
    } 
    

    然后,需要执行质保相关活动的任何对象应实现该接口:

    // Disclaimer: been away from Java for a long time, so please interpret as pseudo-code. 
    
    // Will compile 
    public class Car implements hasWarranty { 
        public void lastWarrantyCheck() { 
         ... need to have this exact function or program won't compile ... 
        } 
        public void checkWarranty() { 
         ... need to have this exact function or program won't compile ... 
        } 
    } 
    
    // Missing one of the required functions defined in hasWarranty 
    public class Bus implements hasWarranty { 
        public void lastWarrantyCheck() { 
         ... need to have this exact function or program won't compile ... 
        } 
    } 
    
  3. 只有常量,真的像在接口中声明的变量是不变的,是由所有共享实现该接口的对象。它们隐含地是“静态最终”。