写这个个人笔记完全是出于记录自己所学,方便本人日后参考,可能很零散。这个笔记是建立在C语言编程基础上,本人学习Java只学习它与C语言不同的地方,或者我在C编程过程中很少用到的地方。所用的教材是Youtube一位达人做的视频:Java编程简明教程 by Thenewboston(Youtube)
每次开一贴,学习Java笔记都记在这里。所写内容都是个人菜鸟级的理解,肯定错误百出。
---------- Oct 14, 2014 ---------
配置Java环境变量:
http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载安装JDK for x86 (32bit) or x64(64bit). 右键“计算机”-属性-高级系统配置-高级-环境变量-xxx的用户变量-(For my backup:)变量名:JAVA_HOME 变量值jdk的目录,例如本人:E:\Program Files\Java\jdk1.7.0_40
变量名: path 变量值: ;%JAVA_HOME%\bin ;%JAVA_HOME%\jre\bin 变量名:classpath 变量值: ;%JAVA_HOME%\lib ;%JAVA_HOME%\lib\tools.jar
<span style="white-space:pre"> </span>JAVA_HOME <span style="white-space:pre"> </span>F:\Program Files\Java\jdk1.8.0_20 //java jdk 1.8 <span style="white-space:pre"> </span>C:\Program Files\Java\jdk1.7.0_67 //java jdk 1.7<span style="white-space: pre;"> </span>path <span style="white-space: pre;"> </span>;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
<span style="white-space: pre;"> </span>classpath <span style="white-space: pre;"> </span>;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
———————–下面检验是否配置成功,运行cmd命令,在出现的对话框输入”java“命令,如果出现以下结果,则表明配置成功。————用法: java [-options] class [args…]
(执行类)
或 java [-options] -jar jarfile [args…]
(执行 jar 文件)
其中选项包括:
-d32 使用 32 位数据模型 (如果可用)
-d64 使用 64 位数据模型 (如果可用)
-client 选择 "client" VM
-server 选择 "server" VM
默认 VM 是 client.
。。。
运行cmd命令,
在出现的对话框输入”javac“命令,出现以下结果,表明配置成功:
C:\Users\LENOVO>javac
用法: javac <options> <source files>
---------- 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() 的使用。
//file: project/apples.java class apples{ public static void main(String args[]){ tuna tunaObj = new tuna(); //create an obj: "tuna" - class name, tunaObj-Obj name, tunaObj.MysimpleMessage(); } }//file: project/tuna.java public class tuna{ public void MysimpleMessage(){ System.out.println("This is class tuna"); } }
2. 题目:创建和使用Scanner对象,以从键盘获取输入:
//file: project/apples.java import java.util.Scanner;class apples{ public static void main(String args[]){ Scanner input = new Scanner(System.in); //creat an scanner obj tuna tunaObj = new tuna(); //create an obj: "tuna" - class name, tunaObj-Obj name,
System.out.println("Enter your name here: "); String name = input.nextLine(); tunaObj.MysimpleMessage(name); }
}
//file: project/tuna.java public class tuna{ public void MysimpleMessage(String name){ System.out.println("Hello" + name); } }
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
//----------------------- tutorial 16 //file: project/apples.java import java.util.Scanner;class apples{ // Create a method named "main" Type"void": public static void main(String args[]){ //Create method body: //Create a Scanner Obj: Scanner input = new Scanner(System.in); //Create a tuna Obj: tuna tunaObj = new tuna(); String name;
System.out.println("Enter your name here: "); //wait usr to input a string: name = input.nextLine(); tunaObj.GetName(name); //call class, use method MysimpleMessage(); tunaObj.saying(); }
}
//file: project/tuna.java public class tuna { private String girlname;
public void GetName(String name){ girlname = name; } public String retName(){ return girlname; } public void saying(){ System.out.printf("your name: %s",retName()); }
}
4. Constructor的使用,用于创建对class_Obj的使用时,同时设置class_Obj的初始参数。比如一个班的学生的姓名,年龄等。
·理解Constructor的作用,出现的位置。它用于在初始化obj时设置参数,所以应该在非主class中。
·Constructor的声明形式:它有点像声明一个method,但是没有返回类型。constructor的名称与class名须一致。Constructor体内多是赋值符。
·副class结构:从本例可以看出,一个副class的结构大致为: class名-> class private variable 声明 -> Constructor声明 -> methods声明 (熟悉基本结构很重要)
·Constructor在主class中被使用时的语法。要注意。
//----------------------- tutorial 17 //file: project/apples.java class apples{ // Create a method named "main" Type"void": public static void main(String args[]){ tuna tunaObj1 = new tuna("Amily",16); tuna tunaObj2 = new tuna("Banry",19);tunaObj1.saying(); tunaObj2.saying(); }
}
//file: project/tuna.java public class tuna{ private String girlname; private int girlage;
public tuna(String name, int age){ //Create constructor, this "tuna" is exactly the name of this class girlname = name; girlage = age; }
/* public void GetNmae(String name){ girlname = name; }*/
public String retName(){ return girlname; } public void saying(){ System.out.printf("Your name is: %s, Age: %d \n", retName(), girlage); }
}
5. Java中使用 " ((条件判断) ? 语句1 :语句2) " 替代并简化if条件判断语句:
//----------------------- tutorial 36/37 Display Regurlar Time //file: project/apples.javaclass apples{ // Create a method named "main" Type"void": public static void main(String args[]){
tuna tunaObj = new tuna(); tunaObj.setTime(14, 56, 20); System.out.println(tunaObj.dispTime()); System.out.println(tunaObj.dispRegTime()); }
}
//file: project/tuna.java public class tuna{ private int hour; private int minute; private int second;
public void setTime(int h, int m, int s){ hour = ((h>=0 && h<=24) ? h : 0); //equivalent with"  if (h>=0 && h<=24)  {hour = h;} else {hour = 0;} minute = ((m>=0 && m<=60) ? m : 0); second = ((s>=0 && s<=60) ? s : 0); } public String dispTime(){ return String.format("%02d:%02d:%02d", hour, minute, second); } public String dispRegTime(){ return String.format("%d:%02d:%02d %s", ((hour<=12) ? hour : hour%12), minute, second, ((hour>12) ? "PM" : "AM")); }
}
6. Java中this关键字,public /private概念。当constructor 中arguments 和 variable 的名称完全一样时候,用this可以指明调用哪一个值。
//----------------------- Tutorial - 38 - Public, Private and this //file: project/apples.java class apples{ // Create a method named "main" Type"void": public static void main(String args[]){tuna tunaObj = new tuna(); System.out.println(tunaObj.dispTime()); //System.out.println(tunaObj.dispRegTime()); tunaObj.setTime(14, 56, 20); System.out.println(tunaObj.dispTime()); //System.out.println(tunaObj.dispRegTime()); }
}
//file: project/tuna.java public class tuna{ private int hour = 1; private int minute = 2; private int second = 3;
public void setTime(int hour, int minute, int second){ this.hour = hour; this.minute = 5; // Use the value of 5 when minute is called by dispTime() method second = 6; // Use the value of 3 when second is called by dispTime() method
// hour = ((h>=0 && h<=24) ? h : 0); // minute = ((m>=0 && m<=60) ? m : 0); // second = ((s>=0 && s<=60) ? s : 0); }
public String dispTime(){ return String.format("%02d:%02d:%02d", hour, minute, second); } public String dispRegTime(){ return String.format("%d:%02d:%02d %s", ((hour<=12) ? hour : hour%12), minute, second, ((hour>12) ? "PM" : "AM")); }
}
7. Constructor 的嵌套使用,适用于constructor输入参数个数不确定的情形:
//----------------------- tutorial 40/41 - Building Objects for Constructors //file: project/apples.java class apples{ public static void main(String[] args){ //create 4 diff objs as there are 4 diff constructor tuna tunaObj0 = new tuna(); tuna tunaObj1 = new tuna(5); tuna tunaObj2 = new tuna(5,13); tuna tunaObj3 = new tuna(5,13,14);//use the objs System.out.printf("%s\n",tunaObj0.toMilitary()); System.out.printf("%s\n",tunaObj1.toMilitary()); System.out.printf("%s\n",tunaObj2.toMilitary()); System.out.printf("%s\n",tunaObj3.toMilitary()); }
}
//file: project/tuna.java public class tuna{ private int hour; private int minute; private int second;
public tuna(){ this(0,0,0); } public tuna(int h){ this(h,0,0); } public tuna(int h, int m){ this(h,m,0); } public tuna(int h, int m, int s){ setTime(h,m,s); } public void setTime(int h, int m, int s){ setHour(h); setMinute(m); setSecond(s); } public void setHour(int h){ hour = ((h>=0 && h<24)? h: 0); } public void setMinute(int m){ minute = ((m>=0 && m<60)? m: 0); } public void setSecond(int s){ second = ((s>=0 && s<60)? s: 0); } public int getHour(){ return hour; } public int getMinute(){ return minute; } public int getSecond(){ return second; } public String toMilitary(){ return String.format("%02d:%02d:%02d",getHour(),getMinute(),getSecond()); }
}
8. 比较复杂一个例子。
1)关于toString() method的使用。在potpie class中,每次声明使用potpie class时,由于其中constructor 语句里具有一个“
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 的配合使用。
//----------------- tutorial 42 toString methods, 43-Composition //file: project/apples.java class apples{ public static void main(String[] args){ potpie potObj = new potpie(4,5,2006); tuna tunaObj = new tuna("Jack",potObj); //take the potObj just built up System.out.println(tunaObj); } }//file: project/tuna.java public class tuna{ private String name; private potpie date;
public tuna(String theName, potpie birthday){ name = theName; date = birthday; } public String toString(){ return String.format("Your name is %s, and birthday is %s", name, date); }
}
//file: project/potpie.java public class potpie {
private int month; private int day; private int year; public potpie(int m, int d, int y){ month = m; day = d; year = y; System.out.printf("The constructor for this is %s \n", this); //this is a reference to the obj we just built whenever we call this class } public String toString(){ return String.format("%d/%d/%d",month,day,year); }
}
9. 枚举类型enum的使用。
//----------------------- tutorial 44/45 Enumeration 枚举 //file: project/apples.java //import java.utile.EnumSet; // enable to use EnumSet.rang(from,to); class apples{ public static void main(String[] args){ for (tuna people : tuna.values()) System.out.printf("%s\t%s\t%s \n", people, people.getDesc(),people.getYear()); System.out.println("\n And now for the range of constants!\n");// for (tuna people: EnumSet.range(tuna.kelsey,tuna.candy)) // pick up part of the members from tuna // System.out.printf("%s\t%s\t%s \n", // people, people.getDesc(),people.getYear());
}
}
//file: project/tuna.java public enum tuna{ // change class into enum // enu constants/obj: bucky("nice","22"), kelsey("cutie","10"), julia("bigmistake", "12"), nicole("italian","13"), candy("different","14"), erin("iwish","16");
private final String desc; // variable(String) private final String year; // build enum constructor tuna(String description, String birthday){ desc = description; year = birthday; } public String getDesc(){ return desc; } public String getYear(){ return year; }
}
10. 关于Static的用法:(此处包含变量的static和method的static)
//----------------------- tutorial 46/47 Static //file: project/apples.java //file: project/apples.java class apples{ public static void main(String[] args){ tuna member1 = new tuna("Megan","Fox"); tuna member2 = new tuna("Natalie","Portman");System.out.println("----------------"); System.out.println(member1.getFirst()); System.out.println(member1.getLast()); System.out.println(member1.getMembers()); System.out.println( tuna.getsMembers()); //getsMembers() is a static method }
}
//file: project/tuna.java public class tuna{ private String first; private String last; private static int members = 0; //static variable, like "in-class globel variable"
public tuna(String fn,String ln) { first = fn; last = ln; members++; System.out.printf("Constructor for %s %s, members in the club: %d\n",first,last,members); } public String getFirst(){ return first; } public String getLast(){ return last; } public int getMembers(){ return members; } public static int getsMembers(){ return members; }
}
11. final的理解和用法:
//----------------------- tutorial 48 final //file: project/apples.java class apples{ public static void main(String[] args){ tuna tunaObj = new tuna(10); tuna tunaObj2= new tuna(1);for(int i = 0; i<3 ; i++){ tunaObj.add(); System.out.printf("%s",tunaObj); } System.out.println("-------------"); for(int i = 0; i<3 ; i++){ tunaObj2.add(); System.out.printf("%s",tunaObj2); } }
}
//file: project/tuna.java public class tuna{ private int sum; private final int NUMBER;
public tuna(int x){ NUMBER = x; // you can only modify it once in one class //NUMBER = 5; //can not change its value again } public void add(){ sum+=NUMBER; } public String toString(){ return String.format("sum = %d\n",sum); }
}
12. Inheritance -- 继承 ,这是我接触到的新概念。 什么是java 的继承? 1)被继承的class叫做super class, super class 中private的变量和method不能被继承。 2)继承的class中如果method名与super class中的method名重合,则使用继承object时,运行继承class中重合的method代码,即以继承class 为准。此谓overwrite.
//----------------------- tutorial 49 Inheritance 继承 //----------------------- tutorial 49 Inheritance 继承 //file: project/apples.java class apples{ public static void main(String[] args){ tuna tunaObj = new tuna(); potpie potpieObj = new potpie();System.out.println("--- tunaObj.eat() ---"); tunaObj.eat(); System.out.println("--- potpieObj.drink() ---"); potpieObj.drink(); System.out.println("--- potpieObj.potpiemethod() ---"); potpieObj.potmethod(); }
}
//file: project/food.java //super class public class food{ public void eat(){ System.out.println("I am eat methods from food"); } public void drink(){ System.out.println("I am drink methods from food"); } private void bite(){ //private methods cannot be inheritance System.out.println("I am bite methods from food"); } }
//file: project/tuna.java public class tuna extends food{ // Inheriance from food public void eat(){ System.out.println("tuna overwritten eas() of food"); } }
//file: project/potpie.java public class potpie extends food{ // Inheritance from food public void potmethod(){ System.out.println("I am potmethod from potpie"); } }
13.