组合和继承

组合:只需要在新的类中产生现有类的对象,新的类是由现有类的对象组成的,这种方法称为组合
继承:按照现有类的类型来创建新类,不需要改变现有类的形式,采用现有类的形式并且在其中添加新的代码。

组合

只需要将对象引用置于新类中即可

  1. class WaterSource{
  2. private String s;
  3. WaterSource(){
  4. System.out.println("WaterSource() -----");
  5. s = "Constructed -----";
  6. }
  7. @Override
  8. public String toString() {
  9. return s;
  10. }
  11. }
  12. public class SprinklerSystem {
  13. private WaterSource source = new WaterSource();//对象引用在新类当中
  14. @Override
  15. public String toString() {
  16. return "SprinklerSystem{
  17. "source=" + source +
  18. }';
  19. }
  20. public static void main(String[] args) {
  21. SprinklerSystem sp = new SprinklerSystem();
  22. System.out.println(sp);
  23. }
  24. }

toString

上方代码,由于编译器需要一个String但只有一个对象的时候,此方法就会被调用,可以将两个String连接在一起,并且将结果返回在sout中

初始化

编译器不会为每一个引用都创建一个默认的对象,初始化引用在以下位置
1.在定义对象的地方。意味着会在构造器被调用之前被初始化
2.在类的构造器中
3.惰性初始化:在正要使用对象之前,在生成对象不值得或者没有必要每次都生成对象的情况下
4.实例初始化

  1. class Soap{
  2. private String s;
  3. Soap(){
  4. System.out.println("soap()");
  5. s = "Constructed";
  6. }
  7. }
  8. public class Bath {
  9. String
  10. s1 = "Happy",
  11. s2 = "Happy",//在定义对象的地方
  12. s3,s4;
  13. private Soap castille;
  14. private int i ;
  15. private float toy;
  16. Bath(){
  17. System.out.println("Inside Bath()");
  18. s3 = "Joy";
  19. toy = 3.14f;//在类的构造器中
  20. castille = new Soap();
  21. {
  22. i = 2;//实例初始化
  23. }
  24. }
  25. @Override
  26. public String toString() {
  27. if (s4 == null){
  28. s4 = "joy";//惰性初始化:在正要使用对象之前,在生成对象不值得或者没有必要每次都生成对象的情况下
  29. }
  30. return "Bath{" +
  31. "s1='" + s1 + '\'' +
  32. ", s2='" + s2 + '\'' +
  33. ", s3='" + s3 + '\'' +
  34. ", s4='" + s4 + '\'' +
  35. ", castille=" + castille +
  36. ", i=" + i +
  37. ", toy=" + toy +
  38. '}';
  39. }
  40. public static void main(String[] args) {
  41. Bath bath = new Bath();
  42. System.out.println(bath);
  43. }
  44. }

继承

继承用extends关键字实现,声明“新类与旧类相似”
当创建一个类的时候如果没有明确从哪个类中继承,否则就是从Object中进行继承的

  1. class Cleanser{
  2. private String s = "Cleanser";
  3. int i = 0;
  4. float f = 3.14f;
  5. public void append(String a){
  6. s +=a;
  7. }
  8. public void dilute(){
  9. append(" dilute() ");
  10. }
  11. public void apply(){
  12. append(" apply() ");
  13. }
  14. public void scrub(){
  15. append(" scrub() ");
  16. }
  17. @Override
  18. public String toString() {
  19. return s;
  20. }
  21. public static void main(String[] args) {
  22. Cleanser cl = new Cleanser();
  23. cl.dilute();
  24. cl.apply();
  25. cl.scrub();
  26. System.out.println(cl);
  27. }
  28. }
  29. public class Detergent extends Cleanser {
  30. int i1 = 0;
  31. public void scrub(){
  32. append(" [Detergent scrub()] ");
  33. }
  34. public static void main(String[] args) {
  35. Cleanser cleanser = new Detergent();
  36. System.out.println(cleanser.i);
  37. System.out.println(cleanser.f);
  38. cleanser.apply();
  39. System.out.println(cleanser);
  40. }
  41. }

初始化基类

当创建了一个导出类的对象的时候,这个对象还包含了一个基类的子对象,这个子对象和直接用基类创建对象是一样的,但是这样是来自于外部,而前者基类的子对象是包装在导出类对象的内部的
JAVA会自动在导出类的构造器中插入对基类构造器的调用
但是如果基类的构造器只有一个带参数的构造器的话,必须用super显示的调用构造器
如果不写的话,基类必须有一个默认构造器

  1. class Art{
  2. Art(){
  3. System.out.println("Art Constructed ------");
  4. }
  5. }
  6. class Drawing extends Art{
  7. Drawing(){
  8. System.out.println("Drawing Constructed");
  9. }
  10. }
  11. public class Cartoon extends Drawing {
  12. public static void main(String[] args) {
  13. new Cartoon();
  14. }
  15. }
  1. class Game{
  2. Game(int i){
  3. System.out.println("Game constructed ---");
  4. }
  5. }
  6. class BoardGame extends Game{
  7. BoardGame(int i){
  8. super(i);//用super
  9. System.out.println("Board Constructed ---");
  10. }
  11. }
  12. public class Chess extends BoardGame {
  13. Chess(int i){
  14. super(i);
  15. System.out.println("Chess Constructed ---");
  16. }
  17. public static void main(String[] args) {
  18. new Chess(20);
  19. }
  20. }

代理

将一个成员对象置于所构造的类的(就像组合一样),但是要在新的类中暴露出来这个成员对象当中的所有方法

  1. class SpaceShipControls{
  2. void up(int velocity){}
  3. void down(int velocity){}
  4. void left(int velocity){}
  5. void right(int velocity){}
  6. void forward(int velocity){}
  7. void back(int velocity){}
  8. void turboBoost(int velocity){}
  9. }
  10. public class SpaceShipDelegation {
  11. private String name;
  12. private SpaceShipControls controls = new SpaceShipControls();
  13. public SpaceShipDelegation(String name){
  14. this.name = name;
  15. }
  16. public void up(int velocity){
  17. controls.up(velocity);
  18. }
  19. public void down(int velocity){
  20. controls.down(velocity);
  21. System.out.println("下降了一百米");
  22. }
  23. public void left(int velocity){
  24. controls.left(velocity);
  25. }
  26. public void right(int velocity){
  27. controls.right(velocity);
  28. }
  29. public void forward(int velocity){
  30. controls.forward(velocity);
  31. }
  32. public void back(int velocity){
  33. controls.back(velocity);
  34. }
  35. public void turboBoost(int velocity){
  36. controls.turboBoost(velocity);
  37. }
  38. public static void main(String[] args) {
  39. SpaceShipDelegation sp = new SpaceShipDelegation("Nasa Ship");
  40. sp.down(100);
  41. }
  42. }

结合使用组合和继承

  1. class Plate{
  2. Plate(int i){
  3. System.out.println("plate constructed ---");
  4. }
  5. }
  6. class DinnerPlate extends Plate{
  7. DinnerPlate(int i){
  8. super(i);
  9. System.out.println("DinnerPlate constructed ---");
  10. }
  11. }
  12. class Utensil{
  13. Utensil(int i){
  14. System.out.println("Utensil constructed ---");
  15. }
  16. }
  17. class Spoon extends Utensil{
  18. Spoon(int i){
  19. super(i);
  20. System.out.println("Spoon Constructed ---");
  21. }
  22. }
  23. class Fork extends Utensil{
  24. Fork(int i){
  25. super(i);
  26. System.out.println("Fork constructed ---");
  27. }
  28. }
  29. class Knife extends Utensil{
  30. Knife(int i){
  31. super(i);
  32. System.out.println("Knife constructed ---");
  33. }
  34. }
  35. class Custom{
  36. Custom(int i){
  37. System.out.println("Custom constructed ---");
  38. }
  39. }
  40. public class PlaceSetting extends Custom{
  41. private Spoon sp;
  42. private Knife kn;
  43. private Fork fo;
  44. private DinnerPlate dp;
  45. public PlaceSetting(int i){
  46. super(i + 1);
  47. sp = new Spoon(i + 2);
  48. fo = new Fork(i + 3);
  49. kn = new Knife(i + 4);
  50. dp = new DinnerPlate(i + 5);
  51. System.out.println("PlateSetting constructed ---");
  52. }
  53. public static void main(String[] args) {
  54. PlaceSetting placeSetting = new PlaceSetting(9);
  55. }
  56. }

清理

先清理子类,再向上清理父类

名称屏蔽

如果基类中有某个多次被重载的方法名称,如果再导出类中重新定义该方法名称并不会屏蔽在基类中的任何版本

  1. class Homer{
  2. char doSth(char c){
  3. System.out.println("doSth (char)");
  4. return c;
  5. }
  6. float doSth(float f){
  7. System.out.println("doSth (float)");
  8. return f;
  9. }
  10. }
  11. class MilHouse{}
  12. class Bart extends Homer{
  13. void doSth(MilHouse m){
  14. System.out.println("doSth (MilHouse)");
  15. }
  16. }
  17. public class Hide{
  18. public static void main(String[] args) {
  19. Bart bart = new Bart();
  20. bart.doSth('x');
  21. bart.doSth(1.0f);
  22. bart.doSth(1);
  23. bart.doSth(new MilHouse());
  24. }
  25. }

在组合和继承之间左选择

组合技术通常在新类中使用现有类的功能而非它的接口这种情形

  1. class Engine{
  2. public void start(){}
  3. public void rev(){}
  4. public void stop(){}
  5. }
  6. class Wheel{
  7. public void inflate(int psi){
  8. System.out.println("哈哈哈");
  9. }
  10. }
  11. class Window{
  12. public void rollUp(){
  13. System.out.println("窗户摇上来了");
  14. }
  15. public void rollDown(){}
  16. }
  17. class Door{
  18. Window window = new Window();
  19. public void open(){}
  20. public void close(){}
  21. }
  22. public class Car {
  23. public Engine engine = new Engine();
  24. public Wheel[] wheels = new Wheel[4];
  25. public Door left = new Door(),
  26. right = new Door();
  27. public Car(){
  28. for (int i = 0 ; i < 4 ; i++){
  29. wheels[i] = new Wheel();
  30. }
  31. }
  32. public static void main(String[] args) {
  33. Car car = new Car();
  34. car.left.window.rollUp();
  35. car.wheels[0].inflate(2);
  36. }
  37. }

protect关键字

对于类用户而言使私有的,对于它的子类来说是可以访问的

  1. class Villain{
  2. private String name;
  3. protected void set(String nm){
  4. this.name = nm;
  5. }
  6. public Villain(String name){
  7. this.name = name;
  8. }
  9. @Override
  10. public String toString() {
  11. return "i am a villain and my name is = " + name;
  12. }
  13. }
  14. public class Orc extends Villain {
  15. private int orcNumber;
  16. public Orc(String name , int orcNumber){
  17. super(name);
  18. this.orcNumber = orcNumber;
  19. }
  20. public void change(String name , int orcNumber){
  21. set(name);
  22. this.orcNumber = orcNumber;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Orc " + orcNumber + ": " + super.toString();
  27. }
  28. public static void main(String[] args) {
  29. Orc orc = new Orc("王博涛", 22);
  30. System.out.println(orc);
  31. orc.change("王庚鑫",25);
  32. System.out.println(orc);
  33. Villain villain = new Villain("hahah");
  34. System.out.println(villain);
  35. }
  36. }

如果父类方法为protected访问权限的话,子类可以扩大为public权限
对静态方法来说,子类可以拥有父类的静态方法,子类同样可以定义与基类一样的static方法,但不属于重写.