- r w x r _ x _ _ _
↑ ↑ ↑
user group other
◎檔案執行 → 一定要有執行的權限才可執行,不能看副檔名來執行。 (Linux)
※ Windows 要看副檔名才可執行。
~# chmod -R g+rwX demodir
※ 權限大小寫,目錄上可執行權限,可以看到目錄內檔案名稱及內容。
Process → 執行緒、排程 Program ---------> process run底下為還蠻常使用指令:
~$ ps aux
lifelong learning and ongoing creation
~# chmod -R g+rwX demodir
※ 權限大小寫,目錄上可執行權限,可以看到目錄內檔案名稱及內容。
Process → 執行緒、排程 Program ---------> process run底下為還蠻常使用指令:
~$ ps aux
// InterfaceMember.java
interface Surfacing{
double pi=3.14159;
double area();
}
class Shap{
protected double x, y;
Shap(double x, double y){
this.x = x;
this.y = y;
}
public String toString(){
return "圖形原點:("+ x +", " + y + ")";
}
}
class Ccircle extends Shap implements Surfacing{
private double r;
public Ccircle(double x, double y, double r){
super(x,y);
this.r = r;
}
public double area(){
return pi * r * r;
}
public String toString(){
return "圓心:(" + x + "," + y + "),半徑:" + r + ",面積:" + area();
}
}
public class InterfaceMember{
public static void main(String args[]){
Ccircle c = new Ccircle(3, 5, 6);
System.out.println(c.toString());
System.out.println("圓周率:" + c.pi);
System.out.println("圓周率:" + Surfacing.pi); // interface不需建立實體也能執行
}
}
/*******************************************************************************************************/// LineShow.java
abstract class LineDemo { //宣告抽象類別
private int length;
LineDemo(int length){
this.length = length;
}
abstract double area(); //抽象方法
int getlength(){ //一般方法
return length;
}
}
public class LineShow extends LineDemo{
LineShow(int length){
super(length);
}
public static void main(String args[]){
LineShow s = new LineShow(10);
System.out.println("area= "+s.area());
//因為 length 是 private 權限,要印出必須是透過 getlength()
System.out.println("length= "+s.getlength());
}
public double area(){ //實作 LineDemo 的 area()
return Math.pow(getlength(),2); //Math.pow(n,a) --> n^a
}
}
// Week.java
public enum Week{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
// WeekEnum.java
public class WeekEnum{
public static void play(Week week){
switch(week){
case Sunday:
System.out.println("Sunday");
break;
case Monday:
System.out.println("Monday");
break;
case Tuesday:
System.out.println("Tuesday");
break;
case Wednesday:
System.out.println("Wednesday");
break;
case Thursday:
System.out.println("Thursday");
break;
case Friday:
System.out.println("Friday");
break;
case Saturday:
System.out.println("Saturday");
break;
default:
System.out.println("DEFAULT");
}
}
public static void main(String args[]){
play(Week.Sunday);
play(Week.Monday);
}
}
// 實作練習 Enum 各取得內容方法
// 需要保留前一練習的檔案 Week.java
// WeekEnum2.java
public class WeekEnum2{
public static void main(String args[]){
System.out.println("=========方法2:透過參照=========");
Week week = Week.Sunday;
System.out.println(week.Sunday);
System.out.println("=========方法3-1:透過 value() / for 迴圈=========");
Week[] week2 = Week.values();
for(int i=0; i < week2.length; i++)
System.out.println(week2[i] + ", ");
System.out.println("=========方法3-2:透過 value() / for-each=========");
for(Week w:Week.values())
System.out.println(w + ", ");
System.out.println("=======方法4-1:利用 valueOf(String s)來取得=======");
System.out.println(Week.valueOf("Sunday"));
System.out.println("=======方法4-2:利用 valueOf(String s)來取得=======");
System.out.println(Week.valueOf(Week.class, "Sunday"));
}
}
// 練習 InnerClass:MyOuterDemo.java
class MyOuter{
class MyInner{
public void foo(){
System.out.println("MyInner foo()");
}
}
}
public class MyOuterDemo{
public static void main(String args[]){
System.out.println("===== 方法一 =====");
MyOuter t = new MyOuter();
MyOuter.MyInner t1 = t.new MyInner();
t1.foo();
System.out.println("===== 方法二 =====");
MyOuter.MyInner t3 = new MyOuter().new MyInner();
t3.foo();
System.out.println("===== 方法三 =====");
new MyOuter().new MyInner().foo();
}
}
// InnerClassDemo.java
class MyOuter{
private static int sx=9;
private int x=7;
class MyInner{
private int x=77;
public void foo(){
int x=777;
System.out.println("Local x =" + x);
//無法印出 //System.out.println("MyInner x=" + MyInner().x);
System.out.println("MyInner x =" + this.x);
System.out.println("MyOuter x =" + MyOuter.this.x);
//無法印出 //System.out.println("MyOuter x=" + MyOuter.x);
System.out.println("MyOuter sx =" + sx);
System.out.println("MyOuter sx =" + MyOuter.sx);
}
}
}
public class InnerClassDemo{
public static void main(String args[]){
new MyOuter().new MyInner().foo();
}
}
// CboxDemo.java
class Cbox{
private int height, width, length;
private Color cr;
Cbox(int height, int width, int length, String str){
this.height = height;
this.width = width;
this.length = length;
cr = new Color(str);
}
void show(){
System.out.println("area = " + height*width*length);
cr.show_color();
}
class Color{ // innerclass
private String color;
Color(String color){
this.color=color;
}
void show_color(){
System.out.println("color = " + color);
}
}
}
public class CboxDemo{
public static void main(String args[]){
Cbox b = new Cbox(3,5,8,"blue");
Cbox c = new Cbox(8,9,13,"yellow");
System.out.println("Box b:");
b.show();
System.out.println("Box c:");
c.show();
}
}
// ShowInner.java
class businessCard{
private company c;
private employee e;
businessCard(String company, String name, String address, String mobile, String email){
c = new company(company);
e = new employee(name, address, mobile, email);
}
void show(){
c.show_company();
e.show_employee();
}
void separator(){
System.out.println("-------------------------------");
}
class employee{
private String name, address, mobile, email;
employee(String n, String a, String m, String e){
name=n;
address=a;
mobile=m;
email=e;
}
void show_employee(){
System.out.println("| 姓名:"+name+" |");
separator();
System.out.println("| 地址:"+address+" |");
separator();
System.out.println("| 行動電話:"+mobile+" |");
separator();
System.out.println("| E-Mail:"+email+"|");
separator();
}
}
class company{
private String myCompany;
company(String myCompany){
this.myCompany = myCompany;
}
void show_company(){
System.out.println(" "+myCompany + "公司員工資料");
separator();
}
}
}
public class ShowInner{
public static void main(String arga[]){
new businessCard("STUST", "Aaron Huang", "Taipei,Taiwan","0987-005986", "kaneju0921@gmail.com").show();
}
}
// MethodInnerClass.java
public class MethodInnerClass{
public static void main(String args[]){
new MethodInnerClass().see();
}
void see(){
class MyInner{
void foo(){
System.out.println("MethodInnerClass foo");
}
}
MyInner mi = new MyInner(); // 注意必須建立 MyInner 實體
mi.foo(); // 才能印出 foo()
}
}
// Date:2015/08/16
// test_0816_1.java
// 以父類別變數存取子類別物件成員
class Circle{
protected double radius;
Circle(double radius){
this.radius = radius;
}
void show(){
System.out.println("area = " + radius*radius);
}
}
class App extends Circle{
private int value;
App(double r, int v){
super(r);
value = v;
}
void show(){
// 要使用 super,才能印出 Circle.show()
super.show();
System.out.println("radius = " + radius + ", value = " + value);
}
}
public class test_0816_1{
public static void main(String args[]){
Circle a = new App(0.2,3);
a.show(); //發生 override,只會印 App.show()
}
}
// Date:2015/08/16
// Demo.java
// 繼承、多型、轉型練習
class Father{
String name = "Father";
String getName(){
return name;
}
String greeting(){
return "Class Father";
}
}
class Son extends Father{
String name = "Son";
String greeting(){
return "Class Son";
}
void foo(){
System.out.println(name); //Son
System.out.println(this.name); //Son
System.out.println(super.name); //Father
System.out.println(((Son)this).name); //Son
System.out.println(((Father)this).name); //Father
System.out.println(((Son)this).greeting()); //Class Son
System.out.println(((Father)this).greeting()); //Class Son
System.out.println(super.greeting()); //Class Father
}
}
public class Demo{
public static void main(String args[]){
new Son().foo();
}
}
~$ mkdir RPG ~$ cd RPG/
// Role.java
public class Role{
protected String name;
protected int level;
protected int blood;
//name
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//level
public void setLevel(int level){
this.level = level;
}
public int getLevel(){
return level;
}
//blood
public void setBlood(int blood){
this.blood = blood;
}
public int getBlood(){
return blood;
}
public String toString(){
return String.format("(%s, %d, %d)%n", this.name, this.level, this.blood);
}
public void fight(){}
}
// Swordman.java
class Swordman extends Role{
public void fight(){
System.out.println("揮劍攻擊");
}
}
//Magician.java
class Magician extends Role{
public void fight(){
System.out.println("魔法攻擊");
}
public void cure(){
System.out.println("魔法治療");
}
}
// Date:2015/08/16
// RJG.java(Role.java, Magician.java, Swordman.java)
public class RPG{
public static void showBlood(Swordman swordman){
System.out.printf("%s 血量 %d %n", swordman.getName(), swordman.getBlood());
}
public static void showBlood(Magician magician){
System.out.printf("%s 血量 %d %n", magician.getName(), magician.getBlood());
}
public static void main(String args[]){
Swordman swordMan = new Swordman();
swordMan.setName("Justin");
swordMan.setLevel(1);
swordMan.setBlood(200);
//System.out.println(swordMan.toString());
System.out.printf("劍士:(%s %d %d)%n", swordMan.getName(), swordMan.getLevel(), swordMan.getBlood());
Magician magician = new Magician();
magician.setName("Monica");
magician.setLevel(1);
magician.setBlood(100);
//System.out.println(magician.toString());
System.out.printf("魔法師:(%s %d %d)%n", magician.getName(),magician.getLevel(), magician.getBlood());
showBlood(swordMan);
showBlood(magician);
}
}
// 練習 interface 及 implements
interface Pet{
String name="cute";
void move();
void skill();
}
public class Dog implements Pet{
// 存取權限一定要大於 interface
public void move(){
System.out.println("移動");
}
public void skill(){
System.out.println("跑跳");
}
public static void main(String args[]){
Dog d = new Dog();
d.skill();
d.move();
}
}
/*----------------------------------------------------------------------------------------------------*/~$ mkdir Ocean ~$ cd Ocean/
//1. Swimmer.java
interface Swimmer{
void swim();
}
//2. Flyer.java
interface Flyer{
void fly();
}
//3. Driver.java
interface Driver extends Swimmer{
void drive();
}
//4. Fish.java
public class Fish implements Swimmer{
protected String name;
public Fish(String name){
this.name = name;
}
public String getName(){
return name;
}
public void swim(){}
}
//5. Airplane.java
public class Airplane implements Flyer{
protected String name;
Airplane(String name){
this.name = name;
}
public void fly(){
System.out.printf("飛機 %s 在飛%n", name);
}
}
//6. Boat.java
public class Boat implements Swimmer{
protected String name;
Boat(String name){
this.name = name;
}
public void swim(){
System.out.printf("%s 小船在水面航行%n", name);
}
}
//7. Human.java
public class Human{
protected String name;
public Human(String name){
this.name = name;
}
public String getName(){
return name;
}
public void swim(){}
}
//8. Helicopter.java
public class Helicopter extends Airplane{
Helicopter(String name){
super(name);
}
public void fly(){
System.out.printf("直升機 %s 在飛%n", name);
}
}
//9. SeaPlan.java
public class SeaPlan extends Airplane implements Swimmer{
SeaPlan(String name){
super(name);
}
public void fly(){
System.out.printf("海上飛機 %s 空中飛行%n", name);
}
public void swim(){
System.out.printf("海上飛機 %s 水面划行%n", name);
}
}
//10. Shark.java
public class Shark extends Fish{
Shark(String name){
super(name);
}
public void swim(){
System.out.printf("鯊魚 %s 游泳 %n", name);
}
}
//11. Anemonefish.java
public class Anemonefish extends Fish{
Anemonefish(String name){
super(name);
}
public void swim(){
System.out.printf("小丑魚 %s 游泳 %n", name);
}
}
//12. Piranha.java
public class Piranha extends Fish{
Piranha(String name){
super(name);
}
public void swim(){
System.out.printf("食人魚 %s 游泳 %n", name);
}
}
//13. FlyingFish.java
public class FlyingFish extends Fish implements Flyer{
FlyingFish(String name){
super(name);
}
public void swim(){
System.out.printf("飛魚 %s 游泳%n", name);
}
public void fly(){
System.out.printf("飛魚 %s 會飛%n",name);
}
}
//14. SwimPlayer.java
public class SwimPlayer extends Human implements Swimmer{
SwimPlayer(String name){
super(name);
}
public void swim(){
System.out.printf("潛水夫 %s 在游泳%n", name);
}
}
//15. DemoOcean.java //主程式
public class DemoOcean{
public static void doSwim(Swimmer swimmer){
swimmer.swim();
}
public static void doFly(Flyer flyer){
flyer.fly();
}
public static void doDriver(Driver driver){
driver.drive();
}
public static void main(String args[]){
SeaPlan s = new SeaPlan("S");
FlyingFish f = new FlyingFish("f");
//Swimmer
doSwim(new Anemonefish("anemonefish"));
doSwim(new Shark("shark"));
doSwim(new Piranha("piranha"));
doSwim(new SwimPlayer("swimmer"));
doSwim(s);
doSwim(f);
//Flyer
doFly(new Helicopter("helicopter"));
doFly(s);
doFly(f);
}
}
練習 interface method()
// 練習 interface method()
// Service.java
interface Other{
void execute();
void doOther();
}
interface Some{
void execute();
void doSome();
}
public class Service implements Some,Other{
public void execute(){
System.out.println("execute");
}
public void doSome(){
System.out.println("doSome");
}
public void doOther(){
System.out.println("doOther");
}
public static void main(String args[]){
Service d = new Service();
d.execute();
d.doSome();
d.doOther();
}
}
// Override 覆寫練習1,印出:
/*
兒子的事業:電腦網路
市值: $100
父親的事業:房地產
市值: $80000000
*/
class Father{
public int money = 80000000;
public void undertaking(){
System.out.println("父親的事業:房地產");
}
}
class Son extends Father{
public int money;
Son (int money){
this.money = money;
}
public void undertaking(){
System.out.println("兒子的事業:電腦網路");
}
public void go(){
undertaking();
System.out.println("市值: $" + money);
super.undertaking();
System.out.println("市值: $" + super.money);
}
}
public class Extends_0802_1{
public static void main(String args[]){
new Son(100).go();
}
}