java基础1

来源:互联网 发布:sqlserver导入数据库 编辑:程序博客网 时间:2024/05/17 22:52

文件结构

Java访问修饰符

com.xiaoming / XiaoMing.java:

  1. package com.xiaoming;   
  2. import java.util.*;   
  3. import com.xiaoqiang.*;   
  4. public class XiaoMing {   
  5.   
  6.     /**  
  7.      * @param args  
  8.      */  
  9.     public static void main(String[] args) {   
  10.         // TODO Auto-generated method stub   
  11.         HashMap hm=new HashMap();   
  12.         Cat cat1=new Cat();   
  13.         System.out.println(cat1.getName());   
  14.     }   
  15. }   
  16.   
  17. class Dog   
  18. {   
  19. }

com.xiaoqiang / Cat.java:

  1. package com.xiaoqiang;   
  2.   
  3. public class Cat {   
  4.     public int a;   
  5.     protected String name;   
  6.     String color;   
  7.     private float price;   
  8.        
  9.     //提供一个访问name的成员方法   
  10.     public String getName()   
  11.     {   
  12.         return this.name;   
  13.     }   
  14. }

com.xiaoqiang / Test2.java:

  1. package com.xiaoqiang;   
  2.   
  3. public class Test2 {   
  4.   
  5.     /**  
  6.      * @param args  
  7.      */  
  8.     public static void main(String[] args) {   
  9.         // TODO Auto-generated method stub   
  10.         Dog dog1=new Dog();   
  11.         System.out.println(dog1.a);   
  12.     }   
  13. }   
  14.   
  15. class Dog   
  16. {   
  17.     public int a;   
  18.     protected String name;   
  19.     String color;   
  20.     private float price;   
  21.        
  22.     public void abc1()   
  23.     {   
  24.         System.out.println(a);   
  25.     }   
  26. }  

Com.XiaoQiang /XiaoQiang.java:

  1. package com.xiaoqiang;   
  2.   
  3. public class XiaoQiang {   
  4.   
  5.     /**  
  6.      * @param args  
  7.      */  
  8.     public static void main(String[] args) {   
  9.            
  10.     }   
  11. }

com.test / Test.java:

  1. /*  
  2.  * 功能:抽象类的必要性  
  3.  */  
  4. package com.test;   
  5.   
  6. public class Test {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.         //Animal an=new Animal();//抽象类不能被实例化   
  13.            
  14.     }   
  15.   
  16. }   
  17.   
  18. abstract class Animal //抽象类   
  19. {   
  20.     String name;   
  21.     int age;   
  22.     //动物会叫   
  23.     /*  
  24.     public void cry()//父类方法不确定性  
  25.     {  
  26.         System.out.println("不知道怎么叫!");  
  27.     }  
  28.     */  
  29.        
  30.     public void aaa()   
  31.     {   
  32.         System.out.println("抽象类中的实现方法");   
  33.     }   
  34.     //抽象类中可以包含实现了的方法   
  35.        
  36.     abstract public void cry();//抽象方法   
  37.     //抽象类的特征:   
  38.     //1.抽象类不能被实例化   
  39.     //2.抽象类可以没有抽象方法   
  40.     //3.包含了abstract(抽象)方法的类,必须声明为abstract   
  41.     //4.抽象方法不能有主体,即不能有{}   
  42. }   
  43.   
  44. class Cat extends Animal   
  45. {   
  46.     //当一个类继承的父类是抽象类的话,就需要在子类在把抽象类   
  47.     //的所有抽象方法全部实现   
  48.        
  49.     //实现   
  50.     public void cry()   
  51.     {   
  52.         System.out.println("猫猫叫。。。");   
  53.     }   
  54. }   

com.test2 / Test.java:

  1. /*  
  2.  * 功能:接口的案例  
  3.  */  
  4.   
  5. package com.test2;   
  6.   
  7. public class Test {   
  8.   
  9.     /**  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {   
  13.         System.out.println(Usb.a);   
  14.            
  15.         //创建一个计算机   
  16.         Computer computer=new Computer();   
  17.         //创建Camera   
  18.         Camera cameral=new Camera();   
  19.         //创建手机   
  20.         Phone phone1=new Phone();   
  21.            
  22.         computer.useUsb(cameral);   
  23.         computer.useUsb(phone1);   
  24.            
  25.     }   
  26. }   
  27.   
  28. //Usb接口   
  29. interface Usb   
  30. {   
  31.     int a=1;   
  32.     public void start();   
  33.     public void stop();   
  34. }   
  35. interface aaa   
  36. {   
  37.     public void Eee();   
  38. }   
  39. interface kkk extends aaa   
  40. {   
  41.     public void cry();   
  42. }   
  43.   
  44. //接口不能被实例化,且接口中不能含有实现 了的方法   
  45. //接口可以理解成更加抽象的抽象方法   
  46. //接口中可以有被初始化的public,static,final类型的变量   
  47. //接口不能继承类,接口可以继承接口   
  48.   
  49. //一个重要的原则:当一个类实现了一个接口,   
  50. //就要求该类把这个接口的所有方法都实现   
  51. class Camera implements Usb,kkk //一个类实现多个方法   
  52. {   
  53.     public void start()   
  54.     {   
  55.         System.out.println("相机,开始工作了!");   
  56.     }   
  57.     public void stop()   
  58.     {   
  59.         System.out.println("相机,停止工作了!");   
  60.     }   
  61.     public void cry()   
  62.     {   
  63.            
  64.     }   
  65.     public void Eee()   
  66.     {   
  67.            
  68.     }   
  69.        
  70. }   
  71.   
  72. class Phone implements Usb   
  73. {   
  74.     public void start()   
  75.     {   
  76.         System.out.println("手机,开始工作了!");   
  77.     }   
  78.     public void stop()   
  79.     {   
  80.         System.out.println("手机,停止工作了!");   
  81.     }   
  82. }   
  83.   
  84. class Computer   
  85. {   
  86.     //开始使用use接口   
  87.     public void useUsb(Usb usb)   
  88.     {   
  89.         usb.start();   
  90.         usb.stop();   
  91.     }   
  92. }  

com.test3 / Test.java:

  1. /*  
  2.  * final修饰类  
  3.  */  
  4. package com.test3;   
  5.   
  6. public class Test {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.            
  13.         Aaa aaa=new Aaa();   
  14.         aaa.show();   
  15.         Bbb bbb=new Bbb();   
  16.         bbb.show();   
  17.     }   
  18. }   
  19.   
  20. //final修饰类,则表示该类不能被继承   
  21. //final class Aaa   
  22. class Aaa   
  23. {   
  24.     int a=0;//如果不给初值,a=? ,在类中的成员变量最好赋个初值   
  25.     //圆周率不希望被别人改,加final修饰   
  26.     //如果一个变量用final修饰,则在定义时必须被初始化,而且后面不能改   
  27.     final float rate_aaa_bbb=3.1415926f;   
  28.     //给方法用final修饰,则表示不可以被修改,不可被 覆盖   
  29.     final public void sendMes()   
  30.     {   
  31.         System.out.println("发送消息");   
  32.     }   
  33.     public void show()   
  34.     {   
  35.         System.out.println("a="+this.a);   
  36.     }   
  37. }   
  38. class Bbb extends Aaa   
  39. {   
  40.     public Bbb()   
  41.     {   
  42.         a++;   
  43.         //rate_aaa_bbb++; //因为rate_aaa_bbb为final类型   
  44.     }   
  45.     /*  
  46.     public void sendMes()  
  47.     {  
  48.         System.out.println("发送消息");  
  49.     }  
  50.     */  
  51. }   
  52.   
  53. interface Fish   
  54. {   
  55.     //该方法实现******   
  56.     public void swimming();   
  57. }   
  58. interface Bird   
  59. {   
  60.     public void fly();   
  61. }   
  62. class Monkey   
  63. {   
  64.     int name;   
  65.     //跳   
  66.     public void jump()   
  67.     {   
  68.         System.out.println("猴子会跳");   
  69.     }   
  70. }   
  71.   
  72. class LittleMonkey extends Monkey implements Fish,Bird   
  73. {   
  74.   
  75.     @Override  
  76.     public void swimming() {   
  77.            
  78.            
  79.     }   
  80.   
  81.     @Override  
  82.     public void fly() {   
  83.            
  84.     }   
  85. }

com..test3 / Mty99.java:

  1. /*  
  2. 99乘法表  
  3. */  
  4.   
  5. package com.test3;   
  6.   
  7. public class Mty99   
  8. {   
  9.     public static void main(String [] args)   
  10.     {   
  11.         for(int i=1;i<=9;i++)   
  12.         {   
  13.             for(int j=1;j<=i;j++)   
  14.             {   
  15.                 if(i==3&&j==3||i==4&&j==3)   
  16.                     System.out.print(" "+j+"*"+i+"="+i*j+" ");   
  17.                 else  
  18.                     System.out.print(j+"*"+i+"="+i*j+" ");   
  19.             }   
  20.             System.out.println("");   
  21.         }   
  22.     }   
  23. }

com.luowei / Demo1.java:

  1. /*  
  2.  * 功能:说明继承的必要性  
  3.  */  
  4.   
  5. package com.luowei;   
  6.   
  7. public class Demo1 {   
  8.   
  9.     /**  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {   
  13.   
  14.         Pupil p1=new Pupil();   
  15.         p1.printName();   
  16.     }   
  17. }   
  18.   
  19. //小学生类   
  20. class Pupil extends Stu   
  21. {   
  22.     /*  
  23.     //定义成员属性  
  24.     private int age;  
  25.     private String name;  
  26.     private float fee;  
  27.     */  
  28.     //交学费   
  29.     public void pay(float fee)   
  30.     {   
  31.         this.fee=fee;   
  32.     }   
  33. }   
  34. //幼儿   
  35. class Kid extends Pupil   
  36. {   
  37. }   
  38.   
  39. //中学生   
  40. class MiddleStu extends Stu   
  41. {   
  42.     /*  
  43.     //定义成员属性  
  44.     private int age;  
  45.     private String name;  
  46.     private float fee;  
  47.     */  
  48.     //交学费   
  49.     public void pay(float fee)   
  50.     {   
  51.         this.fee=fee*0.8f;   
  52.     }   
  53. }   
  54.   
  55. //大学生生   
  56. class CloStu extends Stu   
  57. {   
  58.     /*  
  59.     //定义成员属性  
  60.     private int age;  
  61.     private String name;  
  62.     private float fee;  
  63.     */  
  64.     //交学费   
  65.     public void pay(float fee)   
  66.     {   
  67.         this.fee=fee*0.1f;   
  68.     }   
  69. }   
  70.   
  71. //将学生的共同属性抽象出来,做一个父类   
  72. class Stu   
  73. {   
  74.     protected int age;   
  75.     public String name;   
  76.     public float fee;   
  77.        
  78.     public void printName()   
  79.     {   
  80.         System.out.println("名字:"+this.name);   
  81.     }   
  82.     //如果不希望子类继承某个属性或方法,则将其显示声明为私有    
  83. }  

com.luowei / Demo2.java:

  1. package com.luowei;   
  2.   
  3. import javax.swing.*;   
  4. public class Demo2 extends JFrame {   
  5.     public static void main(String [] args)   
  6.     {   
  7.         //Demo2 demo2=new Demo2();   
  8.            
  9.         Abc abc1=new Abc();   
  10.         System.out.println(abc1.getMax(4,1.2f));   
  11.     }   
  12.     /*  
  13.     public Demo2()  
  14.     {  
  15.         this.setVisible(true);  
  16.         this.setSize(200,200);  
  17.     }  
  18.     */  
  19. }   
  20.   
  21. class Abc   
  22. {   
  23.     //返回较大的整数   
  24.     public int getMax(int i,int j)   
  25.     {   
  26.         if(i>j)   
  27.         {   
  28.             return i;   
  29.         }   
  30.         else  
  31.         {   
  32.             return j;   
  33.         }   
  34.     }   
  35.     public float getMax(float a,float b)//重载,参数类型不同   
  36.     {   
  37.         if(a>b)   
  38.         {   
  39.             return a;   
  40.         }   
  41.         else  
  42.         {   
  43.             return b;   
  44.         }   
  45.     }      
  46.     //如果只是返回类型不同,是不能构成重载的   
  47.     /*  
  48.     public double getMax(float a,float b)//重载,参数类型不同  
  49.     {  
  50.         if(a>b)  
  51.         {  
  52.             return a;  
  53.         }  
  54.         else  
  55.         {  
  56.             return b;  
  57.         }  
  58.     }  
  59.     */  
  60.        
  61.     //如果只是控制访问修饰符号不一样,也不能构成重载   
  62.     /*  
  63.     protected float getMax(float a,float b)//重载,参数类型不同  
  64.     {  
  65.         if(a>b)  
  66.         {  
  67.             return a;  
  68.         }  
  69.         else  
  70.         {  
  71.             return b;  
  72.         }  
  73.     }  
  74.     */  
  75. }

com.luowei / Demo3.java:

  1. package com.luowei;   
  2.   
  3. public class Demo3 {   
  4.   
  5.     public static void main(String [] args)   
  6.     {   
  7.         //创建一只猫   
  8.         Cat cat1=new Cat();   
  9.         cat1.cry();   
  10.         Dog dog1=new Dog();   
  11.         dog1.cry();   
  12.     }   
  13. }   
  14.   
  15. class Animal   
  16. {   
  17.     int age;   
  18.     String name;   
  19.     //都会叫   
  20.     public void cry()   
  21.     {   
  22.         System.out.println("是个动物,不知道 怎么叫唤");   
  23.     }   
  24. }   
  25.   
  26. //猫猫类   
  27. class Cat extends Animal   
  28. {   
  29.     //覆盖就是将父类的方法再重新写一遍,如下边的cry()方法   
  30.     public void cry()   
  31.     {   
  32.         System.out.println("猫猫叫!");   
  33.     }   
  34. }   
  35.   
  36. //狗类   
  37. class Dog extends Animal   
  38. {   
  39.     public void cry()   
  40.     {   
  41.         System.out.println("汪汪叫!");   
  42.     }   
  43. }  

com.luowei / Demo4.java:

  1. /*  
  2.  * 功能:约瑟夫环,数到m的小孩 出圈  
  3.  */  
  4.   
  5. package com.luowei;   
  6.   
  7. public class Demo4 {   
  8.   
  9.     /**  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {   
  13.         CycLink cyclink=new CycLink();   
  14.         cyclink.setLen(5);   
  15.         cyclink.createLink();   
  16.         cyclink.setK(2);//设置从第几个人开始数数   
  17.         cyclink.setM(2);//设置的数m   
  18.         cyclink.show();   
  19.         cyclink.play();   
  20.            
  21.     }   
  22. }   
  23.   
  24. class Child   
  25. {   
  26.     int no;   
  27.     Child nextChild;   
  28.     public Child(int no)   
  29.     {   
  30.         //给一个编号   
  31.         this.no=no;   
  32.     }   
  33. }   
  34.   
  35. class CycLink   
  36. {   
  37.     //先定义一个指向链表第一个小孩的引用   
  38.     //指向第一个小孩的引用,不能动   
  39.     Child firstChild=null;   
  40.     Child temp=null;   
  41.     int len=0;//表示共有多少个小孩   
  42.     int k=0;   
  43.     int m=0;   
  44.        
  45.     //设置小孩的个数   
  46.     public void setLen(int len)   
  47.     {   
  48.         this.len=len;   
  49.     }   
  50.        
  51.     //设置m   
  52.     public void setM(int m)   
  53.     {   
  54.         this.m=m;   
  55.     }   
  56.        
  57.     //设置从第几个人开始数数   
  58.     public void setK(int k)   
  59.     {   
  60.         this.k=k;   
  61.     }   
  62.        
  63.     //开始数数   
  64.     public void play()   
  65.     {   
  66.         Child temp=this.firstChild;   
  67.         //1.先找到开始数数的人   
  68.         for(int i=1;i
  69.         {   
  70.             //因为自己数一下,再找到下一个人,所以将i  
  71.             temp=temp.nextChild;   
  72.         }   
  73.            
  74.         while(this.len!=1)   
  75.         {   
  76.             Child temp2=temp;//用来存放第m-1个孩子   
  77.             //2.数m下   
  78.             for(int j=1;j
  79.             {   
  80.                    
  81.                 if(j==m-1)   
  82.                 {   
  83.                     temp2=temp;   
  84.                 }   
  85.                 temp=temp.nextChild;   
  86.             }   
  87.             /*  
  88.             //找到要出圈的前一个小孩  
  89.             Child temp2=temp;  
  90.             while(temp2.nextChild!=temp)  
  91.             {  
  92.                 temp2=temp2.nextChild;  
  93.             }  
  94.             */  
  95.             //3.将数到m的小孩,退出圈   
  96.             temp2.nextChild=temp.nextChild;   
  97.             //让temp指向下一个数数的小孩   
  98.             temp=temp.nextChild;   
  99.             this.len--;   
  100.         }   
  101.         //最后一个小孩子   
  102.         System.out.println("最后出圈:"+temp.no);   
  103.     }   
  104.        
  105.     //初始化环形链表   
  106.     public void createLink()   
  107.     {   
  108.         for(int i=1;i<=len;i++)   
  109.         {   
  110.             if(i==1)//处理第一个小孩   
  111.             {   
  112.                 //创建第一个小孩   
  113.                 Child ch=new Child(i);   
  114.                 this.firstChild=ch;   
  115.                 this.temp=ch;   
  116.             }   
  117.             else  
  118.             {   
  119.                 if(i==len)//创建最后一个小孩   
  120.                 {   
  121.                     //继续创建小孩子   
  122.                     Child ch=new Child(i);   
  123.                     temp.nextChild=ch;   
  124.                     temp=ch;   
  125.                     temp.nextChild=this.firstChild;   
  126.                 }   
  127.                 else//创建中间小孩   
  128.                 {   
  129.                     //继续创建小孩子   
  130.                     Child ch=new Child(i);   
  131.                     temp.nextChild=ch;   
  132.                     temp=ch;   
  133.                 }   
  134.             }   
  135.         }   
  136.     }   
  137.     //打印该环形链表   
  138.     public void show()   
  139.     {   
  140.         //定义一个跑龙套   
  141.         Child temp=this.firstChild;   
  142.         do  
  143.         {   
  144.             System.out.print(temp.no+" ");   
  145.             temp=temp.nextChild;//指到下一个   
  146.         }while(temp!=this.firstChild);   
  147.     }   
  148. }  

com.luowei / Demo5.java:

  1. /*  
  2.  * 功能:多态的应用  
  3.  */  
  4.   
  5. package com.luowei;   
  6.   
  7. public class Demo5 {   
  8.   
  9.     /**  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {   
  13.         /*  
  14.         Cat2 cat=new Cat2();  
  15.         cat.cry();  
  16.         Dog2 dog=new Dog2();  
  17.         dog.cry();  
  18.         */  
  19.     /*    
  20.         Animal2 an=new Cat2();//多态的应用,创建Animal引用an,在这an是猫  
  21.         an.cry();//通过Animal中的覆盖cry()方法可以实现多态  
  22.         an=new Dog2();//利用多态性,使an的状态成为狗  
  23.         an.cry();  
  24.         //用一种引用可以管理很多的实例  
  25.     */  
  26.         Master master=new Master();   
  27.         master.feed(new Dog2(), new Bone());   
  28.         master.feed(new Cat2(), new Fish());   
  29.     }   
  30. }   
  31.   
  32. //动物类Animal   
  33. class Animal2   
  34. {   
  35.     String name;   
  36.     int age;   
  37.     public String getName() {   
  38.         return name;   
  39.     }   
  40.     public void setName(String name) {   
  41.         this.name = name;   
  42.     }   
  43.     public int getAge() {   
  44.         return age;   
  45.     }   
  46.     public void setAge(int age) {   
  47.         this.age = age;   
  48.     }   
  49.        
  50.     //动物会叫   
  51.     public void cry()   
  52.     {   
  53.         System.out.println("不知道怎么叫");   
  54.     }   
  55.     //方法覆盖(重写)的作用:   
  56.     //1.可以实现多态,实现一个父类引用管理多个子类实例,呈现多种状态   
  57.     //2.可以具体化子类行为。   
  58.        
  59.     //动物可以吃东西   
  60.     public void eat()   
  61.     {   
  62.         System.out.println("不知道 吃什么");   
  63.     }   
  64. }   
  65.   
  66. class Dog2 extends Animal2   
  67. {   
  68.     //狗叫   
  69.     public void cry()   
  70.     {   
  71.         System.out.println("汪汪叫");   
  72.     }   
  73.        
  74.     //狗吃   
  75.     public void eat()   
  76.     {   
  77.         System.out.println("狗喜欢吃骨头");   
  78.     }   
  79. }   
  80.   
  81. class Cat2 extends Animal2   
  82. {   
  83.     //猫自己叫   
  84.     public void cry()   
  85.     {   
  86.         System.out.println("猫猫叫");   
  87.     }   
  88.     //猫吃东西   
  89.     public void eat()   
  90.     {   
  91.         System.out.println("猫喜欢吃鱼");   
  92.     }   
  93. }   
  94.   
  95. //食物   
  96. class Food   
  97. {   
  98.     String name;   
  99.     public void showName()   
  100.     {   
  101.            
  102.     }   
  103. }   
  104.   
  105. //鱼类   
  106. class Fish extends Food   
  107. {   
  108.     public void showName()   
  109.     {   
  110.         System.out.println("鱼");   
  111.     }   
  112. }   
  113.   
  114. //骨头类   
  115. class Bone extends Food   
  116. {   
  117.     public void showName()   
  118.     {   
  119.         System.out.println("骨头");   
  120.     }   
  121. }   
  122.   
  123. //主人类   
  124. class Master   
  125. {   
  126.     //给动物喂食物,使用多态,方法就可以只用一个   
  127.     //这样就不必给每一种具体类型的动物都写一个喂食物的方法了   
  128.     public void feed(Animal2 an,Food f)   
  129.     {   
  130.         an.eat();   
  131.         f.showName();   
  132.     }   

 

原创粉丝点击