1 package org.hanqi.pn0120;
 2 
 3 public class JingTai {
 4     
 5     public int a = 0; //实例属性
 6     public static int b = 0;//静态属性
 7     public void add()
 8     {
 9         a++;
10         System.out.print("a="+a);
11         b++;
12         System.out.println("b="+b);
13         //add1();
14     }
15     public static void add1()
16     {
17         b++;
18 //        JingTai t = new JingTai();
19 //        t.add();
20         //add()
21         //a++;
22     }
23 }
 1 package org.hanqi.pn0120;
 2 
 3 public class DanLi {
 4     
 5     //单例模式
 6     //只被实例化一次
 7     //1.隐藏默认构造方法
 8     private DanLi()
 9     {
10         
11     }
12     
13     //3.准备一个实例
14     private static DanLi danli;
15     
16     //2.提供一个静态方法返回类的实例
17     public static DanLi init()
18     {
19         //实例化唯一的对象
20         //DanLi d = new DanLi();
21         
22         //4.判断是否已被实例化
23         if(danli == null)
24         {
25             danli = new DanLi();
26         }
27         return danli;
28     }
29     
30     private int a = 0;
31     public void add()
32     {
33         a++;
34         System.out.println("a ="+a);
35     }
36 }
 1 package org.hanqi.pn0120;
 2 
 3 public class TestJingTai {
 4 
 5     public static void main(String[] args) {
 6         
 7         for(int i=0;i<10;i++)
 8         {
 9           JingTai t1 = new JingTai();
10           t1.add();
11         }
12           JingTai t2 = new JingTai();
13           t2.add();
14           
15           JingTai.b+=1;
16           System.out.println("JingTai.b="+JingTai.b);
17         JingTai.add1();
18         System.out.println("JingTai.b="+JingTai.b);
19         
20         DanLi d = DanLi.init();
21         d.add();
22         DanLi d1 = DanLi.init();
23         d1.add();
24         DanLi d2 = DanLi.init();
25         d2.add();
26     }
27 }

重载:

 1 package org.hanqi.pn0120;
 2 
 3 public class JiSuanQi {
 4     
 5     //整型加法
 6     public int add(int a,int b)
 7     {
 8         return a+b;
 9     }    
10     //重载  参数个数不同
11     public double add(int c,int d,int e)
12     {
13         return c+d+e;
14     }        
15     //浮点型加法      方法的重载(多态的一种体现)
16     //参数类型不同
17     public double add(double a,double b)
18     {
19         return a+b;
20     }
21 
22     public static void main(String[] args) {
23         
24         JiSuanQi j= new JiSuanQi();
25         System.out.println("20+50="+j.add(20, 50));
26         System.out.println("20.12+50.65="+j.add(20.12, 50.65));
27     }
28 }

继承:

1.父类:

 1 package org.hanqi.pn0120;
 2 
 3 public class Father {
 4     
 5     private String name;
 6     private int age;
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public int getAge() {
14         return age;
15     }
16     public void setAge(int age) {
17         this.age = age;
18     }
19     
20     public Father()
21     {
22         System.out.println("父类的构造方法");
23     }
24     //工作
25     public void work()
26     {
27         System.out.println("我劳动我光荣");
28     }
29 }

2.子类

 1 package org.hanqi.pn0120;
 2 
 3 public class Son extends Father { 
 4     //Object a;所有类的父类
 5     
 6     public Son()
 7     {
 8         System.out.println("子类的构造方法");
 9     }
10     public void sing()
11     {
12         System.out.println("我喜欢唱歌");
13     }
14     //覆盖(重写)
15     public void work()
16     {
17         System.out.println("我不喜欢上班,我要去参加海选");
18     }
19 }

测试类:

 1 package org.hanqi.pn0120;
 2 
 3 public class TestJiCheng {
 4 
 5     public static void main(String[] args) {
 6         // 
 7         Father f = new Father();
 8         f.setName("父亲");
 9         f.setAge(50);
10         System.out.println("名字是:"+f.getName()+"  年龄是:"+f.getAge());
11         f.work();
12         Son s = new Son();
13         s.setName("儿子");
14         s.setAge(20);
15         System.out.println("名字是:"+s.getName()+"  年龄是:"+s.getAge());
16         s.work();
17         s.sing();
18     }
19 }