2012年5月31日 星期四

Java Basic(58)-Abstract and Concrete Classes


abstract public class food { 但類必須是abstract
public abstract void eat(); 你可以有abstract方法
}


In sub-class:

public class tuna extends food{
public void eat(){ 必須是 public
System.out.println("this tuna is great");
}
}

public class potpie extends food{
public void eat(){ 必須是 public
System.out.println("this potpie is great");
}
}

在子類必須方法,因為它們是extendssuper-classfood

Java Basic(57)-Overriding Rules


1.foodmain class(super-class),和potpietuna是子類(sub-class)

2.potpietuna不能有不同的參數 ,必須是相同的在super classfood

3.abstract public class food {
void eat(){
System.out.println("this food is great");
}
}

abstract 意味著你不能創建對象在fatty.classapples.class

Java Basic(56)-Polymorphic Arguments


創建新class稱為fatty



public class fatty {
public void digest(food x){ 方法稱為digestfood X為參數
x.eat();
}

}

in apples.java:
class apples{
public static void main(String[] args){
fatty bucky = new fatty();
food fo = new food(); 因為在fatty.class參數food
food po = new tuna(); 您可以調用potpietuna的價值
bucky.digest(fo);
bucky.digest(po);

}
}

這是運行時的結果: 
this food is great
this tuna is great

Java Basic(55)-Introduction to Polymorphism


public class food {
void eat(){
System.out.println("this food is great");
}
}



public class potpie extends food{
void eat(){
System.out.println("this potpie is great");
}
}

public class tuna extends food{
void eat(){
System.out.println("this tuna is great");
}
}



class apples{
public static void main(String[] args){
food bucky[]=new food[2]; 陣列
bucky[0]=new potpie(); 這裡的新對象
bucky[1]=new tuna(); 這裡的新對象
for(int x=0;x<2;++x){
bucky[x].eat();
}
}
}

由於potpietuna extendsfood,你不需要為tuna.tunaObject(類似的東西
這是運行時的結果: 

this potpie is great
this tuna is great



Java Basic(54)-Event Handler Program


import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame; 導入你必須需要什麼
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{
private JTextField item1;
private JTextField item2; 三個TextFieldpasswordField
private JTextField item3;
private JPasswordField passwordField;
public tuna(){
super("The title");
setLayout(new FlowLayout());
item1 = new JTextField(10); 設置項目(constructor
add(item1);
item2 = new JTextField("enter text here");
add(item2);
item3 = new JTextField("uneditable");
item3.setEditable(false);
add(item3);
passwordField = new JPasswordField("mypass");
add(passwordField);
thehandler handler = new thehandler(); 創建新對象
item1.addActionListener(handler); 設置處理程序(handler)
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class thehandler implements ActionListener{ 必須這樣
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item1)
string=String.format("field 1: %s", event.getActionCommand());
else if(event.getSource()==item2)
string=String.format("field 2: %s", event.getActionCommand());
else if(event.getSource()==item3)
string=String.format("field 3: %s", event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is: %s" , event.getActionCommand());
JOptionPane.showMessageDialog(null, string); 顯示文字TextField
}
}
}

import javax.swing.JFrame;


class apples{
public static void main(String[] args){
tuna bucky = new tuna();
bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bucky.setSize(350, 100);
bucky.setVisible(true);
}

這是運行時的結果: 





}

Java Basic(53)-ActionListener


import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;
public tuna(){
super("The title");
setLayout(new FlowLayout());
item1 = new JTextField(10);
add(item1);
item2 = new JTextField("enter text here");
add(item2);
item3 = new JTextField("uneditable");
item3.setEditable(false);
add(item3);
passwordField = new JPasswordField("mypass");
add(passwordField);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item1)
string=String.format("field 1: %s", event.getActionCommand());
else if(event.getSource()==item2)
string=String.format("field 2: %s", event.getActionCommand());
else if(event.getSource()==item3)
string=String.format("field 3: %s", event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is: %s" , event.getActionCommand());
}
}
}