类:

  1. public class lianxi { 
  2.  double length; 
  3.  double width; 
  4.   
  5.  double area() 
  6.  { 
  7.      return length*width; 
  8.  } 

对象:

 

  1. public class lianxi { 
  2.  double length; 
  3.  double width; 
  4.   
  5.  double area() 
  6.  { 
  7.      return length*width; 
  8.  } 
  9.  public static void main(String[] args) { 
  10.     lianxi lx=new lianxi(); 
  11.     System.out.print(lx.area()); 

封装(成员访问):

  1. /** 
  2.  * 这是封装的类 
  3.  */ 
  4. package Bean; 
  5.  
  6. public class Rectagle { 
  7.  
  8.     private double length; 
  9.     private double width; 
  10.      
  11.     double area() { 
  12.         return length * width; 
  13.     } 
  14.  
  15.     public void display() 
  16.     { 
  17.         System.out.println("长"+length+"\t 宽"+width); 
  18.         System.out.println("长方形面积是"+area()); 
  19.     } 
  20.      
  21.      
  22.     public double getLength() { 
  23.         return length; 
  24.     } 
  25.     public void setLength(double length) { 
  26.         this.length = length; 
  27.     } 
  28.     public double getWidth() { 
  29.         return width; 
  30.     } 
  31.     public void setWidth(double width) { 
  32.         this.width = width; 
  33.     } 
  34.  
  35. /** 
  36.  * 这是调用封装的类 
  37.  */ 
  38. import Bean.Rectagle; 
  39.  
  40. public class lianxi { 
  41.  
  42.     public static void main(String args[]) { 
  43.         Rectagle r = new Rectagle(); 
  44.         r.setLength(10); 
  45.         r.setWidth(5); 
  46.         r.display(); 
  47.     } 

封装(构造方法):

 

  1. /** 
  2.  * 这是封装的类 
  3.  */ 
  4. package Bean; 
  5.  
  6. public class Rectagle { 
  7.  
  8.     private double length; 
  9.     private double width; 
  10.  
  11.  
  12.  
  13.     public Rectagle(double length, double width) { 
  14.         // TODO Auto-generated constructor stub 
  15.         this.length = length;//this关键字表示当前对象 
  16.         this.width = width; 
  17.     } 
  18.  
  19.     public double area() { 
  20.     return this.length * this.width; 
  21.     } 
  22.  
  23.     public double getLength() { 
  24.         return length; 
  25.     } 
  26.  
  27.     public void setLength(double length) { 
  28.         this.length = length; 
  29.     } 
  30.  
  31.     public double getWidth() { 
  32.         return width; 
  33.     } 
  34.  
  35.     public void setWidth(double width) { 
  36.         this.width = width; 
  37.     } 
  38.  
  39.  
  40. /** 
  41.  * 这是调用封装的类 
  42.  */ 
  43. import Bean.Rectagle; 
  44.  
  45. public class lianxi { 
  46.  
  47.     public static void main(String args[]) { 
  48.         Rectagle rectagle = new Rectagle(10.05.0);//调用带参构造方法 
  49.         System.out.println("length="+rectagle.getLength()+" width="+rectagle.getWidth()); 
  50.         System.out.println("area="+rectagle.area()); 
  51.          
  52.     } 

继承(派生):

 

  1. class People { 
  2.     String name; 
  3.     int age; 
  4.     String id = "工人"
  5.  
  6.     public People(String name, int age) {// 构造方法 
  7.         this.name = name; 
  8.         this.age = age; 
  9.     } 
  10.  
  11.     public void talk() { 
  12.         System.out.println("能说话"); 
  13.     } 
  14.  
  15.     public void work() { 
  16.         System.out.println("能工作"); 
  17.     } 
  18.  
  19. /** 
  20.  * 学生类,派生自people类 
  21.  */ 
  22. class Student extends People { 
  23.     String sNo; 
  24.     String id = "学生"
  25.  
  26.     public Student(String name, int age, String sNo) { 
  27.         super(name, age); 
  28.         this.sNo = sNo; 
  29.     } 
  30.  
  31. /** 
  32.  * 教师类,派生自people类 
  33.  *  
  34.  * @author Administrator 
  35.  *  
  36.  */ 
  37.  
  38. class Teather extends People { 
  39.     String sNo; 
  40.     String id = "教师"
  41.  
  42.     public Teather(String name, int age, String sNo) { 
  43.         super(name, age); 
  44.         this.sNo = sNo; 
  45.     } 
  46.  
  47. public class Test { 
  48.     public static void main(String[] args) { 
  49.         Student student=new Student("张三"12"20090221"); 
  50.         System.out.println("name"+student.name+" age"+student.age+" sNo"+student.sNo+" id"+student.id); 
  51.         student.talk(); 
  52.         student.work(); 
  53.         Teather teather=new Teather("lee"32"20090221"); 
  54.         System.out.println("name"+teather.name+" age"+teather.age+" sNo"+teather.sNo+" id"+teather.id); 
  55.         teather.talk(); 
  56.         teather.work(); 
  57.     } 

继承:

 

  1. class A { 
  2.     int x, y; 
  3.     int z = 100
  4.  
  5.     public A(int x, int y) {// 构造方法 
  6.         this.x = x; 
  7.         this.y = y; 
  8.     } 
  9.  
  10.     public void display() { 
  11.         System.out.println("在A里x=" + x + " y=" + y); 
  12.     } 
  13.  
  14. /** 
  15.  * 子类B继承父类 
  16.  *  
  17.  * @author Administrator 
  18.  *  
  19.  */ 
  20. class B extends A { 
  21.     int a, b; 
  22.     int z = 200;// 在子类中定义与父类同名的变量z 
  23.  
  24.     public B(int x, int y, int a, int b) {// 构造方法 
  25.         super(x, y);// 调用父类的构造方法 
  26.         // TODO Auto-generated constructor stub 
  27.         this.a = a; 
  28.         this.b = b; 
  29.     } 
  30.  
  31.     public void display() { 
  32.         super.display();// 重写父类的方法 
  33.         System.out.println("在B里 a=" + a + " b=" + b); 
  34.         System.out.println("子类的变量" + z); 
  35.         System.out.println("父类变量" + super.z); 
  36.     } 
  37.  
  38. public class SuperDemo { 
  39.     public static void main(String[] args) { 
  40.         B a = new B(1234); 
  41.         a.display(); 
  42.  
  43.     } 

多态:

 

  1. import org.omg.CORBA.PUBLIC_MEMBER; 
  2.  
  3. class People { 
  4.     String name; 
  5.     int age; 
  6.     String id = "工人"
  7.  
  8.     // 重载 
  9.     public People()// 无参构造方法 
  10.     { 
  11.         name = ""
  12.         age = 0
  13.     } 
  14.  
  15.     public People(String name, int age) {// 构造方法 
  16.         this.name = name; 
  17.         this.age = age; 
  18.     } 
  19.  
  20.     public void talk() { 
  21.         System.out.println("能说话"); 
  22.     } 
  23.  
  24.     public void work() { 
  25.         System.out.println("能工作"); 
  26.     } 
  27.  
  28. /** 
  29.  * 学生类,派生自people类 
  30.  */ 
  31. class Student extends People { 
  32.     String sNo; 
  33.     String id = "学生"
  34.  
  35.     public Student(String name, int age, String sNo) { 
  36.         super(name, age); 
  37.         this.sNo = sNo; 
  38.     } 
  39.     //重写父类方法 
  40.         public void talk() { 
  41.             System.out.println("学生能回答问题"); 
  42.         } 
  43.         public void work() { 
  44.             System.out.println("学生的工作是上课"); 
  45.         } 
  46.  
  47. /** 
  48.  * 教师类,派生自people类 
  49.  *  
  50.  * @author Administrator 
  51.  *  
  52.  */ 
  53.  
  54. class Teather extends People { 
  55.     String sNo; 
  56.     String id = "教师"
  57.  
  58.     public Teather(String name, int age, String sNo) { 
  59.         super(name, age); 
  60.         this.sNo = sNo; 
  61.     } 
  62. //重写父类方法 
  63.     public void talk() { 
  64.         System.out.println("教师能讲课"); 
  65.     } 
  66.     public void work() { 
  67.         System.out.println("教师的工作是教书"); 
  68.     } 
  69.      
  70.  
  71. public class Test { 
  72.     public static void main(String[] args) { 
  73.         Student student = new Student("张三"12"20090221"); 
  74.         System.out.println("name" + student.name + " age" + student.age 
  75.                 + " sNo" + student.sNo + " id" + student.id); 
  76.         student.talk(); 
  77.         student.work(); 
  78.         Teather teather = new Teather("lee"32"20090221"); 
  79.         System.out.println("name" + teather.name + " age" + teather.age 
  80.                 + " sNo" + teather.sNo + " id" + teather.id); 
  81.         teather.talk(); 
  82.         teather.work(); 
  83.     }