class BadFoodException extends Exception{ }
public class MyException{
static String[] food = {"Apple", "Banana", "Cocoa", "Coffee", "Toast"};
public static void main(String[] args){
try{
checkFood(args[0]);
}catch(BadFoodException e){
System.out.println("I don't like.");
}
}
static void checkFood(String input) throws BadFoodException{
int flag = 0;
for(String s : food){
if(s.equals(input)){
flag = 1;
break;
}
}
if(flag == 1) System.out.println("I like it.");
else throw new BadFoodException();
}
}
==================================
$ java MyException Toast
I don't like.
$ java MyException Apple
I like it.
標籤
二元樹
(1)
字串常數池
(1)
投資
(3)
每月損益
(37)
例外處理
(1)
泛型
(2)
股票
(15)
指標
(5)
英文
(8)
基本資料型別
(1)
期貨
(1)
程式交易
(10)
量化投資
(5)
亂亂寫
(3)
概念
(3)
資料結構
(3)
演算法
(3)
數學
(3)
轉型
(1)
AMA
(1)
ArrayList
(1)
assert
(1)
BeautifulSoup
(1)
C/C++
(8)
casting
(1)
ClassCastException
(1)
classpath
(1)
Collection
(4)
Comparable
(1)
comparTo()
(1)
constructor
(1)
database
(3)
Debian
(1)
Dropbox
(2)
EA
(2)
enum
(1)
equals()
(2)
exception
(3)
extends
(1)
ffmpeg
(1)
final
(1)
Git
(1)
HashMap
(1)
HashSet
(1)
hasNext()
(1)
HTS
(3)
instanceof
(1)
IS-A
(1)
Iterator
(1)
JAVA
(43)
length
(1)
Linux
(31)
List
(1)
Mac
(6)
Map
(1)
ML
(2)
MT4
(6)
MySQL
(2)
next()
(1)
NullPointerException
(1)
Number
(1)
Numpy
(2)
OpenCart
(1)
OpenCV
(3)
OSX
(1)
overloading
(1)
overriding
(3)
pandas
(2)
PHP
(8)
PriorityQueue
(1)
Python
(11)
Queue
(1)
random()
(1)
reverse()
(1)
Samba
(1)
SCJP
(21)
sqrt()
(1)
synchronized
(1)
talib
(1)
ufw
(1)
uTorrent
(1)
var-args
(2)
VHF
(1)
vim
(2)
Yhoo知識+
(4)
2012年9月29日 星期六
2012年9月27日 星期四
exception測試
class Propagate{
public static void main(String[] args){
try{
System.out.println(new Propagate().reverse(""));
}catch(ArithmeticException e){
System.out.println("Input error!");
}finally{
System.out.println("The main method is done.");
}
}
String reverse(String s){
String reverseStr = "";
if(s.length() == 0) throw new ArithmeticException();
else{
for(int i=s.length()-1; i>=0; --i){
reverseStr += s.charAt(i);
}
}
return reverseStr;
}
}
===========
reverse("Hello")
olleH
The main method is done.
reverse("")
Input error!
The main method is done.
例外是隨便挑一個做throw
public static void main(String[] args){
try{
System.out.println(new Propagate().reverse(""));
}catch(ArithmeticException e){
System.out.println("Input error!");
}finally{
System.out.println("The main method is done.");
}
}
String reverse(String s){
String reverseStr = "";
if(s.length() == 0) throw new ArithmeticException();
else{
for(int i=s.length()-1; i>=0; --i){
reverseStr += s.charAt(i);
}
}
return reverseStr;
}
}
===========
reverse("Hello")
olleH
The main method is done.
reverse("")
Input error!
The main method is done.
例外是隨便挑一個做throw
2012年9月24日 星期一
列舉多載method
public class Bridge{
public enum Suits{
CLUBE(20), DIAMONDS(20), HEARTS(30), SPADES(30),
NOTRUMP(40){
public int getValue(int bid){
return (bid - 1) * 30 + 40;
}
};
Suits(int points){
this.points = points;
}
private int points;
public int getValue(int bid){
return points * bid;
}
}
public static void main(String[] args){
System.out.println(Suits.NOTRUMP.getValue(3));
System.out.println(Suits.SPADES + " " + Suits.SPADES.points);
System.out.println(Suits.values());
}
}
=========================
100
SPADES 30
[LBridge$Suits;@4a65e0
public enum Suits{
CLUBE(20), DIAMONDS(20), HEARTS(30), SPADES(30),
NOTRUMP(40){
public int getValue(int bid){
return (bid - 1) * 30 + 40;
}
};
Suits(int points){
this.points = points;
}
private int points;
public int getValue(int bid){
return points * bid;
}
}
public static void main(String[] args){
System.out.println(Suits.NOTRUMP.getValue(3));
System.out.println(Suits.SPADES + " " + Suits.SPADES.points);
System.out.println(Suits.values());
}
}
=========================
100
SPADES 30
[LBridge$Suits;@4a65e0
2012年9月17日 星期一
instanceof測試
class Animal{
public void testIsA(Animal a){
System.out.println("Animal");
}
}
class Dog extends Animal{
public void testIsA(Animal a){ //a是Animal的參考
System.out.println(a instanceof Animal); //但instanceof會看a的物件型別
System.out.println(a instanceof Dog);
//a.show(); //此行會產生錯誤,因為a的型別為Animal,而該class無show函式
}
public void show(){
System.out.println("I am class Dog");
}
}
public class InstanceOfTest{
public static void main(String[] args){
Animal dog1 = new Animal();
Animal dog2 = new Dog();
Dog dog3 = new Dog();
System.out.println(dog1 instanceof Animal); //true; Animal IS-A Animal
System.out.println(dog1 instanceof Dog); //false; Animal IS-A Dog
System.out.println(dog2 instanceof Animal); //true; Dog IS-A Animal
System.out.println(dog2 instanceof Dog); //true; Dog IS-A Dog
System.out.println(dog3 instanceof Animal); //true; Dog IS-A Dog
dog1.testIsA(dog1); //Animal,*****
dog1.testIsA(dog2); //Animal,overriding取決於呼叫的物件
dog1.testIsA(dog3); //Animal,*****
dog1.testIsA(dog1); //Animal,overriding取決於呼叫的物件
dog2.testIsA(dog2); //true, Dog IS-A Animal
//true, Dog IS-A Dog
dog3.testIsA(dog3); //true,true,
}
}
======
結論:
1・父類別物件可以呼叫子類別函式(通常父類別看不見子類別內的成員),但只限於被子類別overriding過的函式。
2・不管傳入函式的引數為何,皆以函式之參數的型別宣告為準。
3・而instanceof會去看參考所指向的物件,並以該物件為準。
public void testIsA(Animal a){
System.out.println("Animal");
}
}
class Dog extends Animal{
public void testIsA(Animal a){ //a是Animal的參考
System.out.println(a instanceof Animal); //但instanceof會看a的物件型別
System.out.println(a instanceof Dog);
//a.show(); //此行會產生錯誤,因為a的型別為Animal,而該class無show函式
}
public void show(){
System.out.println("I am class Dog");
}
}
public class InstanceOfTest{
public static void main(String[] args){
Animal dog1 = new Animal();
Animal dog2 = new Dog();
Dog dog3 = new Dog();
System.out.println(dog1 instanceof Animal); //true; Animal IS-A Animal
System.out.println(dog1 instanceof Dog); //false; Animal IS-A Dog
System.out.println(dog2 instanceof Animal); //true; Dog IS-A Animal
System.out.println(dog2 instanceof Dog); //true; Dog IS-A Dog
System.out.println(dog3 instanceof Animal); //true; Dog IS-A Dog
dog1.testIsA(dog1); //Animal,*****
dog1.testIsA(dog2); //Animal,overriding取決於呼叫的物件
dog1.testIsA(dog3); //Animal,*****
dog1.testIsA(dog1); //Animal,overriding取決於呼叫的物件
dog2.testIsA(dog2); //true, Dog IS-A Animal
//true, Dog IS-A Dog
dog3.testIsA(dog3); //true,true,
}
}
======
結論:
1・父類別物件可以呼叫子類別函式(通常父類別看不見子類別內的成員),但只限於被子類別overriding過的函式。
2・不管傳入函式的引數為何,皆以函式之參數的型別宣告為準。
3・而instanceof會去看參考所指向的物件,並以該物件為準。
2012年9月16日 星期日
父類別函式覆寫
父類別參考指向子類別物件,執行被覆寫之函式
class GameShape{
public void display(){
System.out.println("GameShape");
}
public void motion(){
System.out.println("Motion");
}
}
public class Player1 extends GameShape{
public void display(){
System.out.println("Player1");
}
public void show(){
System.out.println("show");
}
public void showDad(){
super.display();
}
public static void main(String[] args){
GameShape game = new Player1();
Player1 player = new Player1();
game.display();
player.display();
game.motion();
player.motion();
player.showDad();
player.show();
}
}
====================
Player1
Player1
Motion
Motion
GameShape
show
class GameShape{
public void display(){
System.out.println("GameShape");
}
public void motion(){
System.out.println("Motion");
}
}
public class Player1 extends GameShape{
public void display(){
System.out.println("Player1");
}
public void show(){
System.out.println("show");
}
public void showDad(){
super.display();
}
public static void main(String[] args){
GameShape game = new Player1();
Player1 player = new Player1();
game.display();
player.display();
game.motion();
player.motion();
player.showDad();
player.show();
}
}
====================
Player1
Player1
Motion
Motion
GameShape
show
2012年9月11日 星期二
111106_運算式
1.1+2*3+(4+5*6)/(9-7) 2.1*2+3*(4+5)
括號:((1+(2*3))+((4+5*6)/(9-7))) 括號:((1*2)+(3*(4+5)))
前:++1*23/+4*56-97 前:+*12*3+45
後:123*+456*+97-/+ 後:12*345+*+
括號:((1+(2*3))+((4+5*6)/(9-7))) 括號:((1*2)+(3*(4+5)))
前:++1*23/+4*56-97 前:+*12*3+45
後:123*+456*+97-/+ 後:12*345+*+
實作abstract類別
Fruit.java
package food;
public abstract class Fruit{
protected String color;
protected String name;
public void setColor(String str){
color = str;
}
public abstract void show();
}
Apple.java
import food.Fruit;
class Apple extends Fruit{
protected int price;
public Apple(){}
public Apple(int price){
this.price = price;
}
public void setName(String name){
this.name = name;
}
public void setColor(String color){
this.color = color;
}
public void show(){
System.out.println("There is a fruit, it's color is " + color + ",it's an " + name + ".");
System.out.println("And the price of an apple is " + price + ".");
}
public static void main(String[] args){
Apple apple = new Apple(10);
apple.setName("Apple");
apple.setColor("red");
apple.show();
}
}
-------------------------------------------------------------------------
$ java -classpath /Users/superppp/Documents/java Apple
package food;
public abstract class Fruit{
protected String color;
protected String name;
public void setColor(String str){
color = str;
}
public abstract void show();
}
Apple.java
import food.Fruit;
class Apple extends Fruit{
protected int price;
public Apple(){}
public Apple(int price){
this.price = price;
}
public void setName(String name){
this.name = name;
}
public void setColor(String color){
this.color = color;
}
public void show(){
System.out.println("There is a fruit, it's color is " + color + ",it's an " + name + ".");
System.out.println("And the price of an apple is " + price + ".");
}
public static void main(String[] args){
Apple apple = new Apple(10);
apple.setName("Apple");
apple.setColor("red");
apple.show();
}
}
-------------------------------------------------------------------------
$ java -classpath /Users/superppp/Documents/java Apple
There is a fruit, it's color is red,it's an Apple.
And the price of an apple is 10.
訂閱:
文章 (Atom)