java的三大特性,封装,继承,多态

封装 

Java代码  收藏代码
  1. /** 
  2.  * 所谓封装,就是将对象具有的成员变量和成员函数包装和隐藏起来,让外界无法直接使用, 
  3.  * 被封装的成员只能通过某些特定的方式才能访问。  
  4.  * 实现封装有两个步骤: 
  5.  *   1、将不能暴露的成员隐藏起来,我们就不能让其在类的外部被直接访问或赋值。 
  6.  *      实现方法是将该成员定义为私有的,在成员定义前加上private访问权限修饰符。 
  7.  *   2、用公共方法来暴露对该隐藏成员的访问,可以给成员加上public修饰符,将该成员定义为公共的 
  8.  */  
  9. package com.study.feature;  
  10.   
  11. /**    
  12.  *  
  13.  * @className :Package   
  14.  * @package : com.study.feature  
  15.  * @Description :封装性的测试    
  16.  * @author:lgf    
  17.  * @date :2012 三月 12  10:20:35           
  18.  * @version : 1.0 
  19.  */  
  20. public class Package {  
  21.     // 使用private隐藏  
  22.     private String strValue;  
  23.   
  24.     // 通过get和set进行访问  
  25.     public String getStrValue() {  
  26.         return this.strValue;  
  27.     }  
  28.     public void setStrValue(String strValue) {  
  29.         this.strValue = strValue;  
  30.     }  
  31. }  




继承 
父类 ExtendsFather.java 

Java代码  收藏代码
  1. /** 
  2.  * 继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。 
  3.  * 对象的一个新类可以从现有的类中派生。 
  4.  * 1. 为什么要使用继承? 
  5.  *  a.提高程序的扩展性。 
  6.     b.提高了代码的重用性。 
  7.      
  8.    2. 子类能继承到父类的那些方法和属性 
  9.     第一种:所有的属性和方法都被子类继承到了。 
  10.     第二种: 
  11.     a、子类和父类在同一个包下: 
  12.     公有的受保护的属性和方法被子类继承到了。 
  13.     b、子类和父类不在同一个包下: 
  14.     公有的方法和属性被子类继承到了。 
  15.      
  16.    3. 子类的对象能调用父类的那些方法和属性? 
  17.     a、子类和父类在同一个包下: 
  18.     公有的受保护的属性和方法能被子类调用。 
  19.     b、子类和父类不在同一个包下: 
  20.     公有的方法和属性能被子类调用。 
  21.     在类和抽象类中,默认的就是受保护的。 
  22.     在接口中,默认的就是公有的。 
  23.  */  
  24. package com.study.feature;  
  25.   
  26. /**    
  27.  * 父类 
  28.  * @className :ExtendsFather   
  29.  * @package : com.study.feature  
  30.  * @Description :继承测试    
  31.  * @author:lgf    
  32.  * @date :2012 三月 12  10:33:02           
  33.  * @version : 1.0 
  34.  */  
  35. public class ExtendsFather {  
  36.     // 定义不同四种修饰符的属性  
  37.     private     String privateValue;  
  38.     protected   String protectedValue;  
  39.                 String defaultValue;  
  40.     public      String publicValue;  
  41.       
  42.     // 定义不同四种修饰符的方法  
  43.     private void privateFunction(){  
  44.         System.out.println("privateFunction");  
  45.     }  
  46.       
  47.     protected void protectedFunction(){  
  48.         System.out.println("protectedFunction");  
  49.     }  
  50.       
  51.     void defaultFunction(){  
  52.         System.out.println("defaultFunction");  
  53.     }  
  54.       
  55.     public void publicFunction(){  
  56.         System.out.println("publicFunction");  
  57.     }  
  58. }  



同包下的子类 ExtendsChildrenSamePackage.java 

Java代码  收藏代码
  1. package com.study.feature;  
  2. /** 
  3. *  
  4. *  
  5. * @className :ExtendsChildrenSamePackage   
  6. * @package : com.study.feature  
  7. * @Description : 同一个包下面的继承关系    
  8. * @author:lgf    
  9. * @date :2012 三月 12  10:51:23           
  10. * @version : 1.0 
  11.  */  
  12. public class ExtendsChildrenSamePackage extends ExtendsFather{  
  13.     public static void main(String[] args) {  
  14.         ExtendsFather children = new ExtendsChildrenSamePackage();  
  15.         //children.privateValue = "no"; 无法访问到  
  16.         children.defaultValue = "ok";  
  17.         children.protectedValue = "ok";  
  18.         children.publicValue = "ok";  
  19.           
  20.         //除了private修饰的方法,其他都继承到了  
  21.         //children.privateFunction();  
  22.         children.defaultFunction();  
  23.         children.protectedFunction();  
  24.         children.publicFunction();  
  25.     }  
  26. }  



不同包下的子类 ExtendsChildrenOtherPackage.java 


Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.featureSecond;  
  5. import com.study.feature.ExtendsFather;  
  6.   
  7. /**    
  8.  *  
  9.  * @className :ExtendsChildrenOtherPackage   
  10.  * @package : com.study.featureSecond  
  11.  * @Description :不同包下面的继承关系    
  12.  * @author:lgf    
  13.  * @date :2012 三月 12  10:50:47           
  14.  * @version : 1.0 
  15.  */  
  16. public class ExtendsChildrenOtherPackage extends ExtendsFather{  
  17.     public static void main(String[] args) {  
  18.         ExtendsFather children = new ExtendsChildrenOtherPackage();  
  19.         //children.privateValue = "no"; 无法访问到  
  20.         //children.defaultValue = "no"; 无法访问到  
  21.         //children.protectedValue = "no"; 无法访问到  
  22.         children.publicValue= "ok";  
  23.           
  24.         //除了public修饰的方法,其他都未继承到了  
  25.         //children.privateFunction();  
  26.         //children.defaultFunction();  
  27.         //children.protectedFunction();  
  28.         children.publicFunction();  
  29.     }  
  30. }  



重载和重写 ExtendsOverRideLoad.java 

Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.feature;  
  5.   
  6. /**    
  7.  *  
  8.  * @className :ExtendsClass   
  9.  * @package : com.study.feature  
  10.  * @Description :重载和重写    
  11.  * @author:lgf    
  12.  * @date :2012 三月 12  11:00:35           
  13.  * @version : 1.0 
  14.  */  
  15. public class ExtendsOverRideLoad extends ExtendsFather {  
  16.     @Override  
  17.     public void publicFunction() {  
  18.         //super.publicFunction(); 可以调用父类的方法  
  19.         System.out.println("Override publicFunction");  
  20.     }  
  21.       
  22.     public void publicFunction(String str) {  
  23.         //super.publicFunction(); 可以调用父类的方法  
  24.         System.out.println("overload publicFunction");  
  25.     }  
  26.       
  27.     public static void main(String[] args) {  
  28.         ExtendsFather child= new ExtendsOverRideLoad();  
  29.           
  30.         //Override publicFunction  
  31.         child.publicFunction();  
  32.           
  33.         //child.publicFunction("s");  仅仅只能使用到父类有的方法,重载的方法无法调用  
  34.           
  35.         ExtendsOverRideLoad childSecond = new ExtendsOverRideLoad();  
  36.           
  37.         //Override publicFunction  
  38.         childSecond.publicFunction();  
  39.           
  40.         //overload publicFunction  
  41.         childSecond.publicFunction("overLoad");  
  42.     }  
  43. }  



多态 

父类  Animal.java 

Java代码  收藏代码
  1. /** 
  2.  * 1. Java中除了static和final方法外,其他所有的方法都是运行时绑定的 
  3.  * 2. 构造方法是被隐式声明为static方法 
  4.  * 3. 动态绑定 
  5.  *  将一个方法调用和一个方法主体连接到一起称为绑定(Binding)。  
  6.  *  根据绑定的时机不同,可将绑定分为“早期绑定”和“后期绑定”两种。  
  7.  *  如果在程序运行之前进行绑定(由编译器和链接程序完成),称为早期绑定。  
  8.  *  如果在程序运行期间进行绑定,称为后期绑定,后期绑定也称为“动态绑定”或“运行时绑定”。  
  9.  *  在Java中,多态性是依靠动态绑定实现的,即Java虚拟机在运行时确定要调用哪一个同名方法。  
  10.  * 
  11.  *  4. 多态总结 
  12.  *      一、使用父类类型的引用指向子类的对象 
  13.  *      二、该引用只能调用父类中定义的方法和变量 
  14.  *      三、如果子类中重写了父类中的一个方法,那么在调用这个方法的时候, 
  15.  *          将会调用子类中的这个方法;(动态连接、动态调用) 
  16.  *      四、变量不能被重写(覆盖),”重写“的概念只针对方法, 
  17.  *          如果在子类中”重写“了父类中的变量,那么在编译时会报错。 
  18.  *  5. 多态详解 多态是通过:  
  19.  *      5.1 接口 和 实现接口并覆盖接口中同一方法的几不同的类体现的  
  20.  *      2 父类 和 继承父类并覆盖父类中同一方法的几个不同子类实现的. 
  21.  * 
  22.  *  6. 一个类型引用只能引用引用类型自身含有的方法和变量 
  23.  */  
  24. package com.study.feature;  
  25.   
  26. /**    
  27.  * @className :Animal   
  28.  * @package : com.study.feature  
  29.  * @Description :多态的测试    
  30.  * @author:lgf    
  31.  * @date :2012 三月 12  13:50:36           
  32.  * @version : 1.0 
  33.  */  
  34. public class Animal {  
  35.     public void eat(){  
  36.         System.out.println("eating");  
  37.     }  
  38. }  



多态实现 

Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.feature;  
  5.   
  6. /**    
  7.  *  
  8.  * @className :Cat   
  9.  * @package : com.study.feature  
  10.  * @Description :猫    
  11.  * @author:lgf    
  12.  * @date :2012 三月 12  13:54:01           
  13.  * @version : 1.0 
  14.  */  
  15. public class Cat extends Animal{  
  16.     public void eat(){  
  17.         System.out.println("eating fish");  
  18.     }  
  19. }  



Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.feature;  
  5.   
  6. /**    
  7.  *  
  8.  * @className :Dog   
  9.  * @package : com.study.feature  
  10.  * @Description :狗    
  11.  * @author:lgf    
  12.  * @date :2012 三月 12  13:55:38           
  13.  * @version : 1.0 
  14.  */  
  15. public class Dog extends Animal{  
  16.     public void eat(){  
  17.         System.out.println("eating Bone");  
  18.     }  
  19. }  




结果 

Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.feature;  
  5.   
  6. /**    
  7.  *  
  8.  * @className :Main   
  9.  * @package : com.study.feature  
  10.  * @Description :多态测试    
  11.  * @author:lgf    
  12.  * @date :2012 三月 12  13:57:11           
  13.  * @version : 1.0 
  14.  */  
  15. public class Main {  
  16.       
  17.     public static void main(String[] args) {  
  18.         Animal animal = null;  
  19.         animal = new Animal();  
  20.         animal.eat();//eating  
  21.         Animal cat = new Cat();  
  22.         cat.eat();//eating fish  
  23.         Animal dog = new Dog();  
  24.         dog.eat();//eating Bone  
  25.     }  
  26. }  




例子2 

Java代码  收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package com.study.feature;  
  5.   
  6. /**    
  7.  *  
  8.  * @className :Father   
  9.  * @package : com.study.feature  
  10.  * @Description :多态    
  11.  * @author:lgf    
  12.  * @date :2012 三月 12  14:53:41           
  13.  * @version : 1.0 
  14.  */  
  15. public class Father {  
  16.     public void functionOne(){  
  17.         functionSecond();  
  18.     }  
  19.     public void functionSecond(){  
  20.         System.out.println("Father functionSecond");  
  21.     }  
  22. }  



Java代码  收藏代码
    1. /** 
    2.  *  
    3.  */  
    4. package com.study.feature;  
    5.   
    6. /**    
    7.  *  
    8.  * @className :Children   
    9.  * @package : com.study.feature  
    10.  * @Description :测试    
    11.  * @author:lgf    
    12.  * @date :2012 三月 12  14:55:15           
    13.  * @version : 1.0 
    14.  */  
    15. public class Children extends Father{  
    16.     public void functionSecond(){  
    17.         System.out.println("Children functionSecond");  
    18.     }  
    19.       
    20.     public static void main(String[] args) {  
    21.         Father c = new Children();  
    22.         c.functionOne();//Children functionSecond  
    23.     }  
原文地址:https://www.cnblogs.com/caimuqing/p/5398415.html