java面向对象九个经典例子程序

1 Hello world例子

 1 package Example;      //定义自己的包名  
 2    
 3 public class Example1                    //定义一个类  
 4 {  
 5     public static void main(String[] args)          //系统可以执行的main方法,这里是一个公有静态无返回值的方法  
 6     {  
 7         System.out.println("Hello world!");  
 8 //调用java.lang包中的System类的PrintLine方法输出Hello world!  
 9     }  
10 }  

2 类的基本组成示例

 1 package Example;  
 2 class Person  
 3     {  
 4         public int age;             //声明公共字段age  
 5        private String name;        //声明私有字段name,此时name为类的属性,下面通过公有方法进行访问  
 6        public String getName() {  
 7            return name;  
 8        }  
 9        public void setName(String name) {  
10            this.name = name;  
11        }  
12         
13        public void eat()           //定义无参数的eat方法  
14        {  
15           System.out.println("Person can eat");  
16        }  
17        public void eat(String s)   //定义带参数的eat方法,实现方法重载  
18        {  
19           System.out.println("Person can eat"+s);  
20        }  
21        public Person()            //定义无参构造函数,注意无返回值定义,方法与类同名  
22        {  
23        }  
24        public Person(int age, String name) //重写一个带参数构造函数,注意无返回值定义,方法与类同名  
25        {  
26            this.age = age;         //前一个age为对象的字段,由this指定,后一个age为函数形参  
27            this.name = name;       //前一个name为对象的属性,由this指定,因为在本类中可直接访问,后一个name为函数形参  
28            }  
29         
30 }  
31 public class Example2  
32 {  
33     public static void main(String[] args)  
34     {  
35         Person person1 = new Person();  //调用类的无参构造函数  
36     person1.age = 20;               //给对象的公有字段直接赋值  
37     person1.setName("zhangsan");      //必须使用公共方法才能给对象的属性赋值  
38     System.out.println("第一个人信息,姓名:"+person1.getName()+"年龄:"+person1.age);  
39     person1.eat();                  //调用对象的无参方法  
40     Person person2 = new Person(18, "lisi");//调用类的有参构造函数  
41     System.out.println("第二个人信息,姓名:" + person2.getName() + "年龄:" + person2.age);  
42     person2.eat(" 馒头");           //调用对象的有参方法  
43    
44     }  
45 }  

3静态与非静态变量及方法的使用

 1 package Example;  
 2    
 3 class Example3  
 4 {  
 5     public int x;          //非静态变量  
 6     public static int y;     //静态变量  
 7     void method()       //非静态方法  
 8     {  
 9         x = 1;   //正确,非静态方法可以访问非静态成员  
10         y = 1;   //正确,非静态方法可以访问静态成员  
11        System.out.println("实例方法访问:x="+x+" y="+y);  
12     }  
13     static void smethod()     //静态方法  
14     {  
15         //x = 3;  错误,静态方法不能非静态成员  
16         y = 3;   //正确,静态方法可以访问静态成员  
17        System.out.println("静态方法访问:y="+y);  
18     }  
19     public static void main(String[] args)  
20     {  
21     Example3 prog3 = new Example3();//生成类的实例  
22         prog3.method();    //非静态方法通过实例来调用  
23    
24         Example3.smethod();  //静态方法通过类名来调用  
25     }  
26 }  

4 类继承的例子

 1 package Example;  
 2     
 3 class mother  
 4 {  
 5     public static String sex;//成员变量  
 6     public void method1()//父类成员方法1  
 7     {  
 8        System.out.println("母亲的方法1!");  
 9     }  
10     public  void method2() //父类成员方法2  
11     {  
12        System.out.println("母亲的方法2!");  
13     }  
14 }  
15 class boy extends mother //继承  
16 {  
17     public  void method2() //改写父类成员方法,Java中方法均为虚方法  
18     {  
19        System.out.println("我自己的方法2!");  
20     }  
21 }  
22 public class Example4  
23 {  
24     public static void main(String[] args)  
25     {  
26         boy boys = new boy();  
27         boy.sex = "男孩";//静态变量的继承  
28        System.out.println("继承而来的字段sex的值为:"+boy.sex);  
29         boys.method1();//来自父类的方法  
30         boys.method2();//自己改写后的方法  
31     }  

5类的访问修饰符

 1 package Example;  
 2    
 3 class program1  
 4 {  
 5     public int a;      //公用成员  
 6     protected int b; //保护成员  
 7     int c;     //友好成员  
 8     private int d;      //私有成员  
 9     public void method1()  
10     {  
11         a = 1;  //内部访问公用成员,正确  
12         b = 1;  //内部访问保护成员,正确  
13         c = 1;  //内部访问友好成员,正确  
14         d = 1;  //内部访问私有成员,正确  
15        System.out.println("a="+a+",b="+b+",c="+c+",d="+d);  
16     }  
17 }  
18 class program2  
19 {  
20     public void method2()  
21     {  
22         program1 prog1 = new program1();  
23         prog1.a = 2;  
24         //prog1.b=2  //错误,只能在类的内部访问或在它的继承类里访问  
25         prog1.c=2;    // 正确,在同一个程序集里都可以访问  
26         //prog1.d = 2; //错误,只能在它的类的内部访问  
27        System.out.println("另一个类中访问公有成员a="+prog1.a+",友好成员c="+prog1.c);  
28     }  
29 }  
30 class program3 extends program1  
31 {  
32     public void method3()  
33     {  
34          
35         b = 4;   //正确,保护成员可以在它的继承类里访问  
36        System.out.println("子类可以访问受保护成员b="+b);  
37     }  
38 }  
39 public class Example5  
40 {  
41     public static void main(String[] args)  
42     {  
43         program1 prog1 = new program1();  
44         prog1.method1();  
45         program2 prog2 = new program2();  
46         prog2.method2();  
47         program3 prog3 = new program3();  
48         prog3.method3();  
49     }  
50 }    

6抽象类及其实现示例

 1 package Example;  
 2    
 3 //应该注意的是:继承抽象类的类,要求抽象类中的抽象方法要被实例化  
 4 abstract class personClass //抽象类  
 5 {    
 6     public String sex;//变量。  
 7     public abstract void method(); //抽象方法。  
 8 }  
 9 class man extends personClass //以抽象类为模块的类  
10 {    
11     public  void method()//抽象方法的实例化  
12     {  
13        System.out.println("继承抽象类的方法被实现化了");  
14     }  
15 }  
16 public  class Example6  
17 {  
18     public static void main(String[] args)  
19     {  
20         man boys = new man();//产生一个对象  
21         boys.sex = "male";//给对象一个初值  
22         System.out.println(boys.sex);  
23         boys.method();//调用man类中的方法  
24     }  
25 }  

7接口及其实现

 1 package Example;  
 2    
 3 interface Iinterface //定义一个接口  
 4 {  
 5     int i=10;      //定义的变量为fianal类型;  
 6     void method();  // 声明接口方法,但不能有方法体{}  
 7 }  
 8 public class Example7 implements Iinterface    //实现接口  
 9 {  
10     public void method()   //接口的方法在此必须被重写,注意访问修饰符为public  
11     {  
12        System.out.println("接口实现成功!!!");  
13     }  
14     public static void main(String[] args)  
15     {  
16     Example7 prog7 = new Example7();  
17     //prog7.i=10;      不能修改i的值,为final类型  
18         prog7.method();   
19     }  
20 }  

8 抽象类与接口混合编程例子

 1 package Example;  
 2    
 3     interface myInterface  //定义一个接口  
 4     {  
 5         void method1();  
 6    
 7     }  
 8     abstract class abstractClass//定义一个抽象类  
 9     {  
10    
11          public abstract void method2(); //加abstract  
12     }  
13     class shixianlei extends abstractClass implements myInterface //继承抽象类,实现接口。  
14     {  
15         public String st;       //定义自己的字段  
16         public void method1()   //实现接口,注意访问修饰符加public  
17         {  
18            System.out.println("接口方法已加public实现");  
19         }  
20    
21         public void method2()//实现抽象方法  
22         {  
23            System.out.println("抽象类方法已实现");  
24         }  
25    
26     }  
27     public class Example8  
28     {  
29         public static void main(String[] args)  
30         {  
31             shixianlei sxl = new shixianlei();  
32             sxl.st = "实现类的字段";  
33             System.out.println(sxl.st);  
34             sxl.method1();//实现的接口方法调用  
35             sxl.method2();//实现的抽象类的方法调用  
36    
37         }  
38     }  

9 接口回调与多态性

 1 package Example;  
 2    
 3     interface Eat  //定义一个接口  
 4     {  
 5         void eat();  
 6    
 7     }  
 8    
 9      
10     class Cow implements Eat //实现接口。  
11     {  
12         public String name="牛哞哞";       //定义自己的字段  
13         public void eat()   //实现接口,注意访问修饰符加public  
14         {  
15            System.out.println("母牛爱吃青草");  
16         }  
17     }  
18     class Hen implements Eat //实现接口。  
19     {  
20         public String name="鸡咯咯";       //定义自己的字段  
21         public void eat()   //实现接口,注意访问修饰符加public  
22         {  
23            System.out.println("母鸡爱吃小虫");  
24         }  
25     }  
26      
27 public class Example9  
28 {  
29     public static void main(String[] args)  
30     {  
31         //多态的实现,animalEat代表不同对象,表现出不同的行为  
32     Eat animalEat = new Cow(); //注意这种生成实例的方法,此处代表母牛  
33         animalEat.eat();          //调用母牛的方法  
34         //System.out.println(animalEat.name); //不能访问  
35         animalEat=new Hen();       //注意这种生成实例的方法,此处代表母鸡  
36         animalEat.eat();          //调用母鸡的方法  
37         //System.out.println(animalEat.name);//不能访问  
38     }  
39 }  

猜你喜欢

转载自www.cnblogs.com/datacenter/p/11978723.html
今日推荐