1. 关于Class 中对别的class中的method的使用,创建:
题目:main method在apple.java apple class中,引用tuna class中的println(); method, 输出一行字。
[java] view plaincopy
//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对象,以从键盘获取输入:
[java] view plaincopy
//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中:
[java] view plaincopy
//----------------------- 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的使用时,同时设置class的初始参数。比如一个班的学生的姓名,年龄等。
[java] view plaincopy
//----------------------- 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 String retName(){
return girlname;
}
public void saying(){
System.out.printf("Your name is: %s, Age: %d \n", retName(), girlage);
}
}
5. Java中使用 " ((条件判断) ? 语句1 :语句2) " 替代并简化if条件判断语句:
[java] view plaincopy
//----------------------- tutorial 36/37 Display Regurlar Time
//file: project/apples.java
class 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可以指明调用哪一个值。
[java] view plaincopy
//----------------------- 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输入参数个数不确定的情形:
[java] view plaincopy
//----------------------- 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 语句里具有一个“
[java] view plaincopy
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 plaincopy
//----------------- 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的使用。
[java] view plaincopy
//----------------------- 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)
[java] view plaincopy
//----------------------- 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;
}
}