Java学习个人笔记

 

    写这个个人笔记完全是出于记录自己所学,方便本人日后参考,可能很零散。这个笔记是建立在C语言编程基础上,本人学习Java只学习它与C语言不同的地方,或者我在C编程过程中很少用到的地方。所用的教材是Youtube一位达人做的视频:Java编程简明教程 by Thenewboston(Youtube)

    每次开一贴,学习Java笔记都记在这里。所写内容都是个人菜鸟级的理解,肯定错误百出。

 

   ---------- Feb 04, 2013 ---------

    个人理解:Java 的基本架构是通过class建立。class相当于C函数集,每个Class 里面的 method 相当于C的函数。

    1. 关于Class 中对别的class中的method的使用,创建:

        题目:main method在apple.java apple class中,引用tuna class中的println(); method, 输出一行字。
理解:对象的创建与使用。System.out.println() 的使用。

[java] view plain copy
  1. //file: project/apples.java  
  2. class apples{  
  3.     public static void main(String args[]){  
  4.         tuna tunaObj = new tuna();    //create an obj: "tuna" - class name, tunaObj-Obj name,   
  5.         tunaObj.MysimpleMessage();  
  6.     }  
  7. }  
  8.   
  9. //file: project/tuna.java  
  10. public class tuna{  
  11.     public void MysimpleMessage(){  
  12.     System.out.println("This is class tuna");         
  13.     }  
  14. }  


    2. 题目:创建和使用Scanner对象,以从键盘获取输入:

[java] view plain copy
  1. //file: project/apples.java  
  2. import java.util.Scanner;  
  3.   
  4. class apples{  
  5.     public static void main(String args[]){  
  6.         Scanner input = new Scanner(System.in);  //creat an scanner obj  
  7.         tuna tunaObj = new tuna();    //create an obj: "tuna" - class name, tunaObj-Obj name,   
  8.   
  9.         System.out.println("Enter your name here: ");  
  10.         String name = input.nextLine();  
  11.   
  12.         tunaObj.MysimpleMessage(name);  
  13.     }  
  14.   
  15. }  
  16.   
  17. //file: project/tuna.java  
  18. public class tuna{  
  19.     public void MysimpleMessage(String name){  
  20.     System.out.println("Hello" + name);  
  21.     }  
  22. }  

 

    3. 从main method传递值到别的class的method中:
要求:通过 GetName method 把 name 传递进class tuna中,通过tuna中的saying method 内部调用ret_name method 打印出 name.
本实例理解:1、在method内部声明变量和在class内声明变量有何不同。
2、class 内部互相调用method
3、跨 class 调用method

[java] view plain copy
  1. //----------------------- tutorial 16  
  2. //file: project/apples.java  
  3. import java.util.Scanner;  
  4.   
  5. class apples{  
  6.     // Create a method named "main" Type"void":  
  7.     public static void main(String args[]){  
  8.         //Create method body:  
  9.         //Create a Scanner Obj:  
  10.         Scanner input = new Scanner(System.in);  
  11.         //Create a tuna Obj:  
  12.         tuna tunaObj = new tuna();  
  13.         String name;  
  14.           
  15.         System.out.println("Enter your name here: ");  
  16.         //wait usr to input a string:    
  17.         name = input.nextLine();  
  18.         tunaObj.GetName(name);  
  19.           
  20.         //call class, use method MysimpleMessage();  
  21.         tunaObj.saying();  
  22.     }  
  23. }  
  24.   
  25. //file: project/tuna.java  
  26. public class tuna {  
  27.         private String girlname;  
  28.           
  29.         public void GetName(String name){  
  30.             girlname = name;  
  31.         }  
  32.           
  33.         public String retName(){  
  34.             return girlname;  
  35.         }  
  36.           
  37.         public void saying(){  
  38.             System.out.printf("your name: %s",retName());         
  39.         }  
  40. }  


    4. Constructor的使用,用于创建对class的使用时,同时设置class的初始参数。比如一个班的学生的姓名,年龄等。

[java] view plain copy
  1. //----------------------- tutorial 17  
  2. //file: project/apples.java  
  3. class apples{  
  4.     // Create a method named "main" Type"void":  
  5.     public static void main(String args[]){  
  6.         tuna tunaObj1 = new tuna("Amily",16);  
  7.         tuna tunaObj2 = new tuna("Banry",19);  
  8.           
  9.         tunaObj1.saying();  
  10.         tunaObj2.saying();  
  11.                   
  12.     }     
  13. }  
  14.   
  15. //file: project/tuna.java  
  16. public class tuna{  
  17.     private String girlname;  
  18.     private int girlage;  
  19.       
  20.     public tuna(String name, int age){    //Create constructor, this "tuna" is exactly the name of this class  
  21.         girlname = name;  
  22.         girlage  = age;  
  23.     }  
  24.       
  25. /*  public void GetNmae(String name){ 
  26.         girlname = name;         
  27.     }*/  
  28.       
  29.     public String retName(){  
  30.         return girlname;  
  31.     }  
  32.       
  33.     public void saying(){  
  34.         System.out.printf("Your name is: %s, Age: %d \n", retName(), girlage);  
  35.           
  36.     }  
  37. }  

 

     5.  Java中使用 " ((条件判断) ? 语句1 :语句2) " 替代并简化if条件判断语句:

[java] view plain copy
  1. //----------------------- tutorial 36/37 Display Regurlar Time  
  2. //file: project/apples.java  
  3.   
  4. class apples{  
  5.     // Create a method named "main" Type"void":  
  6.     public static void main(String args[]){  
  7.           
  8.         tuna tunaObj = new tuna();  
  9.         tunaObj.setTime(145620);  
  10.         System.out.println(tunaObj.dispTime());  
  11.         System.out.println(tunaObj.dispRegTime());  
  12.   
  13.     }     
  14. }  
  15.   
  16. //file: project/tuna.java  
  17. public class tuna{  
  18.     private int hour;  
  19.     private int minute;  
  20.     private int second;  
  21.       
  22.     public void setTime(int h, int m, int s){  
  23.         hour = ((h>=0 && h<=24) ? h : 0);    //equivalent with"  if (h>=0 && h<=24)  {hour = h;} else {hour = 0;}   
  24.         minute = ((m>=0 && m<=60) ? m : 0);  
  25.         second = ((s>=0 && s<=60) ? s : 0);  
  26.     }  
  27.       
  28.     public String dispTime(){  
  29.         return String.format("%02d:%02d:%02d", hour, minute, second);  
  30.     }  
  31.     public String dispRegTime(){  
  32.         return String.format("%d:%02d:%02d  %s",   
  33.                 ((hour<=12) ? hour : hour%12), minute, second, ((hour>12) ? "PM" : "AM"));  
  34.     }  
  35. }  


    6.  Java中this关键字,public /private概念。当constructor 中arguments 和 variable 的名称完全一样时候,用this可以指明调用哪一个值。

[java] view plain copy
  1. //----------------------- Tutorial - 38 - Public, Private and this  
  2. //file: project/apples.java  
  3. class apples{  
  4.     // Create a method named "main" Type"void":  
  5.     public static void main(String args[]){  
  6.           
  7.         tuna tunaObj = new tuna();  
  8.           
  9.         System.out.println(tunaObj.dispTime());  
  10.         //System.out.println(tunaObj.dispRegTime());  
  11.           
  12.         tunaObj.setTime(145620);  
  13.           
  14.         System.out.println(tunaObj.dispTime());  
  15.         //System.out.println(tunaObj.dispRegTime());  
  16.   
  17.     }     
  18. }  
  19.   
  20. //file: project/tuna.java  
  21. public class tuna{  
  22.     private int hour = 1;  
  23.     private int minute = 2;  
  24.     private int second = 3;  
  25.       
  26.     public void setTime(int hour, int minute, int second){  
  27.         this.hour = hour;  
  28.         this.minute = 5;    // Use the value of 5 when minute is called by dispTime() method  
  29.         second = 6;         // Use the value of 3 when second is called by dispTime() method  
  30.   
  31. //      hour = ((h>=0 && h<=24) ? h : 0);  
  32. //      minute = ((m>=0 && m<=60) ? m : 0);  
  33. //      second = ((s>=0 && s<=60) ? s : 0);  
  34.     }  
  35.       
  36.     public String dispTime(){  
  37.         return String.format("%02d:%02d:%02d", hour, minute, second);  
  38.     }  
  39.     public String dispRegTime(){  
  40.         return String.format("%d:%02d:%02d  %s",   
  41.                 ((hour<=12) ? hour : hour%12), minute, second, ((hour>12) ? "PM" : "AM"));  
  42.     }  
  43. }  

 

    7. Constructor 的嵌套使用,适用于constructor输入参数个数不确定的情形:

[java] view plain copy
  1. //----------------------- tutorial 40/41 - Building Objects for Constructors  
  2. //file: project/apples.java  
  3. class apples{  
  4.     public static void main(String[] args){  
  5.         //create 4 diff objs as there are 4 diff constructor  
  6.         tuna tunaObj0 = new tuna();  
  7.         tuna tunaObj1 = new tuna(5);  
  8.         tuna tunaObj2 = new tuna(5,13);  
  9.         tuna tunaObj3 = new tuna(5,13,14);  
  10.           
  11.         //use the objs  
  12.         System.out.printf("%s\n",tunaObj0.toMilitary());  
  13.         System.out.printf("%s\n",tunaObj1.toMilitary());  
  14.         System.out.printf("%s\n",tunaObj2.toMilitary());  
  15.         System.out.printf("%s\n",tunaObj3.toMilitary());  
  16.     }  
  17.   
  18. }  
  19.   
  20. //file: project/tuna.java  
  21. public class tuna{  
  22.     private int hour;  
  23.     private int minute;  
  24.     private int second;  
  25.       
  26.     public tuna(){  
  27.         this(0,0,0);  
  28.     }  
  29.     public tuna(int h){  
  30.         this(h,0,0);  
  31.     }  
  32.     public tuna(int h, int m){  
  33.         this(h,m,0);  
  34.     }  
  35.     public tuna(int h, int m, int s){  
  36.         setTime(h,m,s);  
  37.     }  
  38.           
  39.     public void setTime(int h, int m, int s){  
  40.         setHour(h);  
  41.         setMinute(m);  
  42.         setSecond(s);  
  43.     }  
  44.     public void setHour(int h){  
  45.         hour = ((h>=0 && h<24)? h: 0);  
  46.     }  
  47.     public void setMinute(int m){  
  48.         minute = ((m>=0 && m<60)? m: 0);  
  49.     }  
  50.     public void setSecond(int s){  
  51.         second = ((s>=0 && s<60)? s: 0);  
  52.     }  
  53.     public int getHour(){  
  54.         return hour;  
  55.     }  
  56.     public int getMinute(){  
  57.         return minute;  
  58.     }  
  59.     public int getSecond(){  
  60.         return second;  
  61.     }  
  62.     public String toMilitary(){  
  63.         return String.format("%02d:%02d:%02d",getHour(),getMinute(),getSecond());  
  64.     }  
  65. }  

 

     8. 比较复杂一个例子。

1)关于toString() method的使用。在potpie class中,每次声明使用potpie class时,由于其中constructor 语句里具有一个“

[java] view plain copy
  1. System.out.printf("The constructor for this is %s \n"this);  

” ,所以每次声明都会打印一句The constructor for this is .xxx ,xxx的内容就调用this. this 会指向the following "toString()" method. 

        2)在tuna class中声明并使用了一个 potpie object,这个object是由第6行的语句传递进去的,第16行的(potpie birthday)语句定义了它的入口。

        3)tuna class 继续使用toString method来服务 apples class 中 main method 中 System.out.println(tunaObj);语句对它的调用。

        明显感觉我还对toString() method 的使用理解不够深入。包括 this 和 toString 的配合使用。

[java] view plain copy
  1. //----------------- tutorial 42 toString methods, 43-Composition  
  2. //file: project/apples.java  
  3. class apples{  
  4.     public static void main(String[] args){  
  5.         potpie potObj = new potpie(4,5,2006);  
  6.         tuna tunaObj = new tuna("Jack",potObj); //take the potObj just built up  
  7.         System.out.println(tunaObj);  
  8.     }  
  9. }  
  10.   
  11. //file: project/tuna.java  
  12. public class tuna{  
  13.     private String name;  
  14.     private potpie date;  
  15.       
  16.     public tuna(String theName, potpie birthday){  
  17.         name = theName;  
  18.         date = birthday;  
  19.     }  
  20.       
  21.     public String toString(){  
  22.         return String.format("Your name is %s, and birthday is %s", name, date);  
  23.     }  
  24. }  
  25.   
  26. //file: project/potpie.java  
  27. public class potpie {  
  28.       
  29.     private int month;  
  30.     private int day;  
  31.     private int year;  
  32.       
  33.     public potpie(int m, int d, int y){  
  34.         month = m;  
  35.         day   = d;  
  36.         year  = y;  
  37.           
  38.         System.out.printf("The constructor for this is %s \n"this);  
  39.         //this is a reference to the obj we just built whenever we call this class  
  40.     }  
  41.     public String toString(){  
  42.         return String.format("%d/%d/%d",month,day,year);  
  43.     }  
  44. }  


    

    9. 枚举类型enum的使用。

[java] view plain copy
  1. //----------------------- tutorial 44/45 Enumeration 枚举  
  2. //file: project/apples.java  
  3. //import java.utile.EnumSet;   // enable to use EnumSet.rang(from,to);  
  4. class apples{  
  5.     public static void main(String[] args){  
  6.         for (tuna people : tuna.values())  
  7.             System.out.printf("%s\t%s\t%s \n",   
  8.                     people, people.getDesc(),people.getYear());  
  9.         System.out.println("\n And now for the range of constants!\n");  
  10.           
  11. //      for (tuna people: EnumSet.range(tuna.kelsey,tuna.candy))   // pick up part of the members from tuna  
  12. //          System.out.printf("%s\t%s\t%s \n",   
  13. //                  people, people.getDesc(),people.getYear());  
  14.           
  15.           
  16.     }  
  17. }  
  18.   
  19. //file: project/tuna.java  
  20. public enum tuna{            // change class into enum  
  21.     // enu constants/obj:  
  22.     bucky("nice","22"),  
  23.     kelsey("cutie","10"),  
  24.     julia("bigmistake""12"),  
  25.     nicole("italian","13"),  
  26.     candy("different","14"),  
  27.     erin("iwish","16");  
  28.       
  29.     private final String desc;  // variable(String)  
  30.     private final String year;  
  31.       
  32.     // build enum constructor  
  33.     tuna(String description, String birthday){  
  34.         desc = description;  
  35.         year = birthday;  
  36.     }  
  37.       
  38.     public String getDesc(){  
  39.         return desc;  
  40.     }  
  41.     public String getYear(){  
  42.         return year;  
  43.     }  
  44. }  

 

 

10. 关于Static的用法:(此处包含变量的static和method的static)

[java] view plain copy
  1. //----------------------- tutorial 46/47 Static  
  2. //file: project/apples.java  
  3. //file: project/apples.java  
  4. class apples{  
  5.     public static void main(String[] args){  
  6.         tuna member1 = new tuna("Megan","Fox");  
  7.         tuna member2 = new tuna("Natalie","Portman");  
  8.           
  9.         System.out.println("----------------");  
  10.           
  11.         System.out.println(member1.getFirst());  
  12.         System.out.println(member1.getLast());  
  13.         System.out.println(member1.getMembers());    
  14.         System.out.println(   tuna.getsMembers()); //getsMembers() is a static method  
  15.           
  16.     }  
  17. }  
  18.   
  19. //file: project/tuna.java  
  20. public class tuna{  
  21.     private String first;  
  22.     private String last;  
  23.     private static int members = 0;   //static variable, like "in-class globel variable"  
  24.   
  25.     public tuna(String fn,String ln) {  
  26.         first = fn;  
  27.         last  = ln;  
  28.         members++;  
  29.         System.out.printf("Constructor for %s %s, members in the club: %d\n",first,last,members);  
  30.     }  
  31.     public String getFirst(){  
  32.         return first;  
  33.     }  
  34.     public String getLast(){  
  35.         return last;  
  36.     }  
  37.     public int getMembers(){  
  38.         return members;  
  39.     }  
  40.     public static int getsMembers(){  
  41.         return members;  
  42.     }  
  43. }  

 

    11. final的理解和用法:

[java] view plain copy
  1. //----------------------- tutorial 48 final  
  2. //file: project/apples.java  
  3. class apples{  
  4.     public static void main(String[] args){  
  5.         tuna tunaObj = new tuna(10);  
  6.         tuna tunaObj2= new tuna(1);  
  7.           
  8.         for(int i = 0; i<3 ; i++){  
  9.             tunaObj.add();  
  10.             System.out.printf("%s",tunaObj);  
  11.         }  
  12.         System.out.println("-------------");  
  13.         for(int i = 0; i<3 ; i++){  
  14.             tunaObj2.add();  
  15.             System.out.printf("%s",tunaObj2);  
  16.         }  
  17.     }  
  18. }  
  19.   
  20. //file: project/tuna.java  
  21. public class tuna{  
  22.     private int sum;  
  23.     private final int NUMBER;  
  24.   
  25.     public tuna(int x){  
  26.         NUMBER = x;  // you can only modify it once in one class  
  27.         //NUMBER = 5;  //can not change its value again  
  28.     }  
  29.     public void add(){  
  30.         sum+=NUMBER;  
  31.     }  
  32.     public String toString(){  
  33.         return String.format("sum = %d\n",sum);  
  34.     }  
  35. }  

 

    12. Inheritance -- 继承 ,这是我接触到的新概念。 什么是java 的继承?  1)被继承的class叫做super class, super class 中private的变量和method不能被继承。  2)继承的class中如果method名与super class中的method名重合,则使用继承object时,运行继承class中重合的method代码,即以继承class 为准。此谓overwrite.

[java] view plain copy
  1. //----------------------- tutorial 49 Inheritance 继承  
  2. //----------------------- tutorial 49 Inheritance 继承  
  3. //file: project/apples.java  
  4. class apples{  
  5.     public static void main(String[] args){  
  6.         tuna tunaObj = new tuna();  
  7.         potpie potpieObj = new potpie();  
  8.   
  9.         System.out.println("--- tunaObj.eat() ---");  
  10.         tunaObj.eat();  
  11.         System.out.println("--- potpieObj.drink() ---");  
  12.         potpieObj.drink();  
  13.         System.out.println("--- potpieObj.potpiemethod() ---");  
  14.         potpieObj.potmethod();  
  15.     }  
  16. }  
  17.   
  18. //file: project/food.java  //super class  
  19. public class food{  
  20.     public void eat(){  
  21.         System.out.println("I am eat methods from food");  
  22.     }  
  23.     public void drink(){  
  24.         System.out.println("I am drink methods from food");  
  25.     }  
  26.     private void bite(){     //private methods cannot be inheritance  
  27.         System.out.println("I am bite methods from food");  
  28.     }  
  29. }  
  30.   
  31. //file: project/tuna.java  
  32. public class tuna extends food{ // Inheriance from food  
  33.     public void eat(){  
  34.         System.out.println("tuna overwritten eas() of food");         
  35.     }  
  36. }  
  37.   
  38. //file: project/potpie.java  
  39. public class potpie extends food{ // Inheritance from food  
  40.     public void potmethod(){  
  41.         System.out.println("I am potmethod from potpie");  
  42.     }  
  43. }  


    13.

你可能感兴趣的:(Java学习)