我读了很多关于“接口”和“抽象类”的文章在面向对象的编程中,我们需要抽象吗?
基本上,我们在谈论对象的特性时使用“抽象类”。
而我们使用“接口”,当我们调查什么对象能够做。
但它仍然让我感到困惑,所以我为自己做了一个练习的例子。
所以现在我想到一个对象的货物;
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.因此,在真正的编程项目世界中,我们是否真的像上面这样做?对我来说,我认为没关系设计,我们符合货物的基本特征。
如果我们设置“专用标识”,那么,我们却不能因为它是私人使用“身份证”在任何子类这个变量,所以是意味着我们在抽象类定义的每个变量必须是公共/保护?
有人可以举一些合适的例子,让我的货物可以实现一些界面吗?
public interface registration{ public void lastWarrantyCheck(); }
,但似乎不太适合在这里...
我们不经常内部接口定义变量,请我们?
我试着在OOP上获得更多的意义。原谅我很长的问题。