static void getClassInfo(Object o) {
Class c = o.getClass();
// 取得套件代表物件
Package p = c.getPackage();
out.printf("package %s;%n", p.getName());
// 取得型態修飾,像是public、final
int m = c.getModifiers();
out.print(Modifier.toString(m) + " ");
// 如果是介面
if (Modifier.isInterface(m)) {
out.print("interface ");
} else {
out.print("class ");
}
out.println(c.getName() + " {");
//-----------------------
// 取得宣告的資料成員代表物件
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
// 顯示權限修飾,像是public、protected、private
out.print("\t"
+ Modifier.toString(field.getModifiers()));
// 顯示型態名稱
out.print(" "
+ field.getType().getName() + " ");
// 顯示資料成員名稱
out.println(field.getName() + ";");
}
out.println("----------------------");
//-----------------------
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
// 顯示權限修飾,像是public、protected、private
out.print("\t"
+ Modifier.toString(
method.getModifiers()));
// 顯示返回值型態名稱
out.print(" "
+ method.getReturnType().getName() + " ");
// 顯示方法名稱
out.println(method.getName() + "();");
}
out.println("}");
}
2019年2月3日 星期日
2019年1月16日 星期三
swing 的組織想法
// 中間協調者
public class Observeable<T> {
// 要通知的組件
public ArrayList<Observe> elements;
public void emit(String eventName, T data){
}
}
public interface Observe<T> {
public void on(String eventName);
public void off(String eventName);
public void notify(String eventName, T data);
}
public class Element<T, E> implements Observe<E> {
// swing 元件
public T x;
@Override
public void on(String eventName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void off(String eventName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void notify(String eventName, E data) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public class Observeable<T> {
// 要通知的組件
public ArrayList<Observe> elements;
public void emit(String eventName, T data){
}
}
public interface Observe<T> {
public void on(String eventName);
public void off(String eventName);
public void notify(String eventName, T data);
}
public class Element<T, E> implements Observe<E> {
// swing 元件
public T x;
@Override
public void on(String eventName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void off(String eventName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void notify(String eventName, E data) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
2018年12月22日 星期六
java promise
https://my.oschina.net/andylucc/blog/608499
http://ifeve.com/promise-future-callback/
https://github.com/hprose/hprose-java/wiki/%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E2%80%94%E2%80%94Promise
https://juejin.im/post/5b126065e51d4506bd72b7cc
https://www.infoq.cn/article/design-patterns-promise https://www.cnkirito.moe/future-and-promise/
interface Closure<E, T> {
public T callback(E in);
public void error(Error err);
}
public class Promise<E> {
protected int status = 0;
protected ArrayList<Closure> jobList = new ArrayList<>();
protected Closure<Promise, Void> job;
protected E data;
public Promise(Closure job){
this.job = job;
// 暴露 resolve, reject
this.job.callback(this);
}
private Promise() {
}
public void resolve(E e){
}
public void reject(String err){
}
protected void reject(Error err){
}
public Promise done(Closure c){
return (new Promise<E>());
}
public Promise error(Closure c){
return (new Promise<E>());
}
}
http://ifeve.com/promise-future-callback/
https://github.com/hprose/hprose-java/wiki/%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E2%80%94%E2%80%94Promise
https://juejin.im/post/5b126065e51d4506bd72b7cc
https://www.infoq.cn/article/design-patterns-promise https://www.cnkirito.moe/future-and-promise/
interface Closure<E, T> {
public T callback(E in);
public void error(Error err);
}
public class Promise<E> {
protected int status = 0;
protected ArrayList<Closure> jobList = new ArrayList<>();
protected Closure<Promise, Void> job;
protected E data;
public Promise(Closure job){
this.job = job;
// 暴露 resolve, reject
this.job.callback(this);
}
private Promise() {
}
public void resolve(E e){
}
public void reject(String err){
}
protected void reject(Error err){
}
public Promise done(Closure c){
return (new Promise<E>());
}
public Promise error(Closure c){
return (new Promise<E>());
}
}
2018年12月21日 星期五
java 非同步做法
public interface Callback<T, E> {
// in: 回傳的資料
public void back(T in);
// 錯誤時要做的事
// err 錯誤訊息
public void error(E err);
}
//----------------------------------------
public class Server_1 {
public Thread asyncGetData(Callback callback) {
out.println("server start");
Thread th_1 = new Thread(() -> {
int time = (int) (Math.random() * 5000);
out.printf("server thread start(%s)\n", time);
try {
Thread.sleep(time);
// 回傳資料
callback.back(time);
out.printf("server thread end(%s)\n", time);
} catch (InterruptedException ex) {
callback.error(ex);
}
});
th_1.start();
return th_1;
}
}
//----------------------------------------
public class Client_1 {
// 資料交換用
int switchData_int;
public void getData(){
out.println("client.getData()");
Server_1 server_1 = new Server_1();
int gg;
Thread th_1 = server_1.asyncGetData(new Callback<Integer, Error>() {
@Override
public void back(Integer in) {
out.println("callback.back");
// 閉包只適用於套嵌的 class
Client_1.this.switchData_int = in;
}
@Override
public void error(Error err) {
}
});
try {
th_1.join();
} catch (InterruptedException ex) {
out.println(ex);
}
out.printf("result(%s)\n------------\n", this.switchData_int);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Client_1 cl = new Client_1();
cl.getData();
cl.getData();
}
}
// in: 回傳的資料
public void back(T in);
// 錯誤時要做的事
// err 錯誤訊息
public void error(E err);
}
//----------------------------------------
public class Server_1 {
public Thread asyncGetData(Callback callback) {
out.println("server start");
Thread th_1 = new Thread(() -> {
int time = (int) (Math.random() * 5000);
out.printf("server thread start(%s)\n", time);
try {
Thread.sleep(time);
// 回傳資料
callback.back(time);
out.printf("server thread end(%s)\n", time);
} catch (InterruptedException ex) {
callback.error(ex);
}
});
th_1.start();
return th_1;
}
}
//----------------------------------------
public class Client_1 {
// 資料交換用
int switchData_int;
public void getData(){
out.println("client.getData()");
Server_1 server_1 = new Server_1();
int gg;
Thread th_1 = server_1.asyncGetData(new Callback<Integer, Error>() {
@Override
public void back(Integer in) {
out.println("callback.back");
// 閉包只適用於套嵌的 class
Client_1.this.switchData_int = in;
}
@Override
public void error(Error err) {
}
});
try {
th_1.join();
} catch (InterruptedException ex) {
out.println(ex);
}
out.printf("result(%s)\n------------\n", this.switchData_int);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Client_1 cl = new Client_1();
cl.getData();
cl.getData();
}
}
2018年11月13日 星期二
2018年11月10日 星期六
java callback
/**
*
* @author xman
*/
public class Tt {
String name = "......";
void query(Tool t) {
t.set(this);
}
public static void main(String[] args) {
Tt tt = new Tt();
tt.query(new Tool<Tt>() {
public void set(Tt o) {
out.println(o.name);
}
});
tt.query((o) -> {
// error
out.println(o.name);
});
}
}
interface Tool<T> {
public void set(T in);
}
java 非同步呼叫 callback
public class Ajax_1 {
Callback callback;
String name;
void query(Callback callback) {
this.callback = callback;
// 非同步
Thread t = new Thread(() -> {
// 取得查詢
String name = (new SyncSqlQuery()).query();
Ajax_1.this.name = name;
Ajax_1.this.callback.callback();
});
t.start();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Ajax_1 a = new Ajax_1();
int x = 5;
a.query(() -> {
// 及時命令
out.println(a.name);
});
a.query(new Callback(){
public void callback(){
// 及時命令
out.println(a.name);
}
});
}
}
//-------------------------------------------------------
public interface Callback {
void callback();
}
//-------------------------------------------------------
public class SyncSqlQuery {
public static int id = 0;
String query(){
return "SyncSqlQuery_" + id++;
}
}
//////////////////////////////////////////////////////////////////////////
/**
*
* @author xman
*/
public class Ajax_1 {
String name;
void query(Callback callback) {
Thread t = new Thread(() -> {
// 取得查詢
String name = (new SyncSqlQuery()).query();
Ajax_1.this.name = name;
});
t.start();
try {
t.join();
} catch (InterruptedException ex) {
out.println(ex);
}
callback.callback();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Ajax_1 a = new Ajax_1();
int x = 5;
a.query(() -> {
out.println(a.name);
});
a.query(new Callback(){
public void callback(){
out.println(a.name);
}
});
}
}
java inner-class 閉包
// 運用 innerClass
public class T_4 {
String name;
String getName() {
return name;
}
//----------------------------
class A {
public void setName(String name) {
T_4.this.name = name;
}
}
//----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
T_4.A a = t.new A();
a.setName("Xman");
out.println(t.getName());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 運用 lambda
// 比較方便偷懶
interface Callback {
// callback 內容隨你寫....讚
public void callback(String name);
}
//-------------------------------------------
public class T_4 {
String name;
String getName() {
return name;
}
// 送出一個 callback
// 來設定 name
Callback getCallback() {
// lambda 生成一個 匿名 class implements Callback
return ((name) -> {
T_4.this.name = name;
});
}
// ----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
// 藉由 callback 設定 t
t.getCallback().callback("Xyz");
out.println(t.getName());
}
}
public class T_4 {
String name;
String getName() {
return name;
}
//----------------------------
class A {
public void setName(String name) {
T_4.this.name = name;
}
}
//----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
T_4.A a = t.new A();
a.setName("Xman");
out.println(t.getName());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 運用 lambda
// 比較方便偷懶
interface Callback {
// callback 內容隨你寫....讚
public void callback(String name);
}
//-------------------------------------------
public class T_4 {
String name;
String getName() {
return name;
}
// 送出一個 callback
// 來設定 name
Callback getCallback() {
// lambda 生成一個 匿名 class implements Callback
return ((name) -> {
T_4.this.name = name;
});
}
// ----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
// 藉由 callback 設定 t
t.getCallback().callback("Xyz");
out.println(t.getName());
}
}
2018年11月3日 星期六
Java中對象的類型判斷
Java中對象的類型判斷
instanceof
判斷一個對象是否是一個類的實例,用Java中自帶的關鍵字instanceof似乎可以做到(僅從關鍵字名稱上可以猜測出),如下面的代碼:
public static void main(String args[]) {
Object i = new Integer(7);
if (i instanceof Number) {
System.out.println("Integer i is a Number");
} else {
System.out.println("Integer i isn't a Number");
}
if (i instanceof Serializable) {
System.out.println("Integer i is a Serializable");
} else {
System.out.println("Integer i isn't a Serializable");
}
if (i instanceof Integer) {
System.out.println("Integer i is an Integer");
} else {
System.out.println("Integer i isn't an Integer");
}
if (i instanceof Float) {
System.out.println("Integer i is a Float");
} else {
System.out.println("Integer i isn't a Float");
}
}
類定義部分為:
public abstract class Number implements java.io.Serializable {}
public final class Integer extends Number implements Comparable<Integer> {}
運行結果:
Console Output :
Integer i is a Number
Integer i is a Serializable
Integer i is an Integer
Integer i isn't a Float
然而好像和預期的不太一樣,能看出,使用該關鍵字不僅可以判斷對象是否是某個類的實例,甚至連該類繼承的基類和實現的接口也都能夠被識別為true,雖然這樣在邏輯上沒有任何問題,是可以把Integer當做一個Number來看來用,也當做一個Serializable來看來用,但是這樣返回的結果就沒有針對性了,太過於模糊,我們如果僅僅需要i instanceof Integer為true該怎麼做呢?
Class.equals
還好在Java有一個叫做Class的類,這是一個用來描述類信息的類,我們如果要精確判斷一個對象是否是具體的一個類的實例,可以這麼做:
public static void main(String args[]) {
Object i = new Integer(7);
if (i.getClass().equals(Number.class)) {
System.out.println("Integer i is a Number");
} else {
System.out.println("Integer i isn't a Number");
}
if (i.getClass().equals(Serializable.class)) {
System.out.println("Integer i is a Serializable");
} else {
System.out.println("Integer i isn't a Serializable");
}
if (i.getClass().equals(Integer.class)) {
System.out.println("Integer i is an Integer");
} else {
System.out.println("Integer i isn't an Integer");
}
if (i.getClass().equals(Float.class)) {
System.out.println("Integer i is a Float");
} else {
System.out.println("Integer i isn't a Float");
}
}
instanceof
判斷一個對象是否是一個類的實例,用Java中自帶的關鍵字instanceof似乎可以做到(僅從關鍵字名稱上可以猜測出),如下面的代碼:
public static void main(String args[]) {
Object i = new Integer(7);
if (i instanceof Number) {
System.out.println("Integer i is a Number");
} else {
System.out.println("Integer i isn't a Number");
}
if (i instanceof Serializable) {
System.out.println("Integer i is a Serializable");
} else {
System.out.println("Integer i isn't a Serializable");
}
if (i instanceof Integer) {
System.out.println("Integer i is an Integer");
} else {
System.out.println("Integer i isn't an Integer");
}
if (i instanceof Float) {
System.out.println("Integer i is a Float");
} else {
System.out.println("Integer i isn't a Float");
}
}
類定義部分為:
public abstract class Number implements java.io.Serializable {}
public final class Integer extends Number implements Comparable<Integer> {}
運行結果:
Console Output :
Integer i is a Number
Integer i is a Serializable
Integer i is an Integer
Integer i isn't a Float
然而好像和預期的不太一樣,能看出,使用該關鍵字不僅可以判斷對象是否是某個類的實例,甚至連該類繼承的基類和實現的接口也都能夠被識別為true,雖然這樣在邏輯上沒有任何問題,是可以把Integer當做一個Number來看來用,也當做一個Serializable來看來用,但是這樣返回的結果就沒有針對性了,太過於模糊,我們如果僅僅需要i instanceof Integer為true該怎麼做呢?
Class.equals
還好在Java有一個叫做Class的類,這是一個用來描述類信息的類,我們如果要精確判斷一個對象是否是具體的一個類的實例,可以這麼做:
public static void main(String args[]) {
Object i = new Integer(7);
if (i.getClass().equals(Number.class)) {
System.out.println("Integer i is a Number");
} else {
System.out.println("Integer i isn't a Number");
}
if (i.getClass().equals(Serializable.class)) {
System.out.println("Integer i is a Serializable");
} else {
System.out.println("Integer i isn't a Serializable");
}
if (i.getClass().equals(Integer.class)) {
System.out.println("Integer i is an Integer");
} else {
System.out.println("Integer i isn't an Integer");
}
if (i.getClass().equals(Float.class)) {
System.out.println("Integer i is a Float");
} else {
System.out.println("Integer i isn't a Float");
}
}
2018年10月25日 星期四
java String, char 間的轉換
String str = "abc";
char[] bm;
// String 轉 char[]
bm = str.toCharArray();
// char[] 轉 String
str = String.valueOf(bm);
char[] bm;
// String 轉 char[]
bm = str.toCharArray();
// char[] 轉 String
str = String.valueOf(bm);
簡化 java System.out.print.*
import static java.lang.System.out;
package com.xyz.util;
import java.io.*;
/**
*
* @author xman
*/
public class Out {
public static void println(Object obj) {
System.out.println(obj);
}
//--------------------------------------------------------------------------
// 换行输出
public static void println() {
System.out.println();
}
//--------------------------------------------------------------------------
// 不换行输出,
public static void print(Object obj) {
System.out.print(obj);
}
//--------------------------------------------------------------------------
public static void printf(String format, Object... args) {
System.out.printf(format, args);
}
}
2018年6月13日 星期三
java extend 的問題
public class A {
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
訂閱:
文章 (Atom)
