P182
package JavaBookstudy; class Person { } public class javaDemo { public static void main(String args[]) { Object obj = new Person() ; if (obj instanceof Person) { Person per = (Person) obj ; } } }
P182
package JavaBookstudy; class Person { } public class javaDemo { public static void main(String args[]) { Object obj = new int[] {1,2,3} ; if (obj instanceof int[]) { int [] data = (int []) obj ; for (int temp : data) { System.out.print(temp + "、"); } } } }
P182
package JavaBookstudy; class Person { private String name ; private int age ; public Person(String name, int age) { this.name = name ; this.age = age ; } @Override public String toString() { return "姓名:" + this.name + "、年龄:" + this.age ; } } public class javaDemo { public static void main(String args[]) { Person per = new Person("李双双",20) ; System.out.println(per); } }
P184
package JavaBookstudy; class Person extends Object { private String name ; private int age ; public Person(String name , int age) { this.name = name ; this.age = age ; } public boolean equals(Object obj) { if(!(obj instanceof Person)){ return false ; } if(obj == null){ return false ; } if(obj == this){ return true ; } Person per = (Person) obj ; return this.name.equals(per.name) && this.age == per.age ; } } public class javaDemo { public static void main(String args[]) { Person perA = new Person ("李双双", 20) ; Person perB = new Person ("李双双", 20) ; System.out.println(perA.equals(perB)); } }
P188
package JavaBookstudy; abstract class Message{ private String type ; public abstract String getConnectInfo() ; public void setType(String type) { this.type = type ; } public String getType() { return this.type ; } } class DatabaseMessage extends Message { @Override public String getConnectInfo() { return "【" + super.getType() + "】数据库连接信息。" ; } } public class javaDemo { public static void main(String args[]) { Message msg = new DatabaseMessage() ; msg.setType("MLDN") ; System.out.println(msg.getConnectInfo()) ; } }
P189
package JavaBookstudy; abstract class Message{ private String type ; public abstract String getConnectInfo() ; public Message(String type) { this.type = type ; } public String getType() { return this.type ; } } class DatabaseMessage extends Message { public DatabaseMessage (String type) { super(type) ; } @Override public String getConnectInfo() { return "【" + super.getType() + "】数据库连接信息。" ; } } public class javaDemo { public static void main(String args[]) { Message msg = new DatabaseMessage("MLDN") ; System.out.println(msg.getConnectInfo()) ; } }
P190
package JavaBookstudy; abstract class Message{ public abstract String getInfo() ; public static Message getInstance () { return new DatabaseMessage() ; } } class DatabaseMessage extends Message { @Override public String getInfo() { return "MLDN数据库连接信息。" ; } } public class javaDemo { public static void main(String args[]) { Message msg = Message.getInstance() ; System.out.println(msg.getInfo()) ; } }
P191
package JavaBookstudy; abstract class Action { public static final int EAT = 1 ; public static final int SLEEP = 5 ; public static final int WORK = 10 ; public void command (int code) { switch (code) { case EAT : { this.eat(); break; } case SLEEP : { this.sleep(); break; } case WORK : { this.work(); break; } case EAT + SLEEP + WORK : { this.eat(); this.sleep(); this.work(); break; } } } public abstract void eat() ; public abstract void sleep() ; public abstract void work() ; } class Robot extends Action { @Override public void eat () { System.out.println("机器人需要接通电源充电。"); } @Override public void sleep () {} @Override public void work () { System.out.println("机器人需要按照固定的套路进行工作。"); } } class Person extends Action { @Override public void eat () { System.out.println("饿的时候安静的坐下吃饭。"); } @Override public void sleep () { System.out.println("安静的躺下,慢慢的睡着,而后做着美梦。"); } @Override public void work () { System.out.println("人类是高级脑类动物,在工作中不断学习与成长。"); } } class Pig extends Action { @Override public void eat () { System.out.println("吃食槽中饲料。"); } @Override public void sleep () { System.out.println("倒地就睡。"); } @Override public void work () {} } public class javaDemo { public static void main(String args[]) { Action robotAction = new Robot() ; Action personAction = new Person() ; Action pigAction = new Pig() ; System.out.println("------------- 机器人行为 -------------"); robotAction.command(Action.SLEEP) ; robotAction.command(Action.WORK) ; System.out.println("------------- 人类行为 -------------"); personAction.command(Action.SLEEP+Action.EAT+Action.WORK) ; System.out.println("------------- 猪类行为 -------------"); pigAction.work(); pigAction.eat() ; } }
P193
package JavaBookstudy; class Int { private int data ; public Int(int data) { this.data = data ; } public int intValue() { return this.data ; } } public class javaDemo { public static void main(String args[]) { Object obj = new Int(10) ; int x = ((Int) obj).intValue() ; System.out.println(x*2); } }
P195
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Double obj = new Double(10.1) ; double num = obj.doubleValue() ; System.out.println(num*num); } }
P195
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Boolean obj = new Boolean(true) ; boolean flag = obj.booleanValue() ; System.out.println(flag); } }
P196
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Integer obj = 10 ; int num = obj ; obj ++ ; System.out.println(num * obj) ; } }
P196
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Object obj = 19.2 ; double num = (Double) obj ; System.out.println(num * 2) ; } }
P197
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Integer x = new Integer(10) ; Integer y = 10 ; Integer z = 10 ; System.out.println(x == y) ; System.out.println(x == z) ; System.out.println(z == y) ; System.out.println(x.equals(y)) ; } }
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { Integer numA1 = 100 ; Integer numA2 = 100 ; System.out.println(numA1 == numA2) ; Integer numB1 = 130 ; Integer numB2 = 130 ; System.out.println(numB1 == numB2) ; System.out.println(numB1.equals(numB2)) ; } }
P198
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { String str = "123" ; int num = Integer.parseInt(str) ; System.out.println(num * num); } }
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { String strA = "true" ; boolean flagA = Boolean.parseBoolean(strA) ; System.out.println(flagA); String strB = "www.mldn.cn、魔乐科技" ; boolean flagB = Boolean.parseBoolean(strB) ; System.out.println(flagB); } }
P199
package JavaBookstudy; public class javaDemo { public static void main(String args[]) { int num = 100 ; String str = num + "" ; System.out.println(str.length()) ; String strA = String.valueOf(num) ; System.out.println(strA.length()); } }
P200
package JavaBookstudy; interface IMessage { public static final String INFO = "www.mldn.cn" ; public abstract String getInfo() ; } class MessageImpl implements IMessage { @Override public String getInfo() { return "魔乐科技软件学院:www.mldn.cn" ; } } public class javaDemo { public static void main(String args[]) { IMessage msg = new MessageImpl() ; System.out.println(msg.getInfo()); } }
P200
package JavaBookstudy; interface IMessage { public static final String INFO = "www.mldn.cn" ; public abstract String getInfo() ; } interface IChannel { public abstract boolean connect() ; } class MessageImpl implements IMessage,IChannel { @Override public String getInfo() { if(this.connect()) { return "魔乐科技软件学院”:www.mldn.cn" ; } return "【默认信息】" + IMessage.INFO ; } @Override public boolean connect() { return true ; } } public class javaDemo { public static void main(String args[]) { IMessage msg = new MessageImpl() ; System.out.println(msg.getInfo()) ; } }
P202
package JavaBookstudy; interface IMessage { public static final String INFO = "www.mldn.cn" ; public abstract String getInfo() ; } interface IChannel { public abstract boolean connect() ; } abstract class DatabaseAbstract { public abstract boolean getDatabaseConnection() ; } class MessageImpl extends DatabaseAbstract implements IMessage,IChannel { @Override public String getInfo() { if(this.connect()) { if(this.getDatabaseConnection()) { return "【数据库消息】魔乐科技软件学院”:www.mldn.cn" ; } else { return "数据库消息无法访问" ; } } return "【默认消息】" + IMessage.INFO ; } @Override public boolean connect() { return true ; } @Override public boolean getDatabaseConnection() { return true ; } } public class javaDemo { public static void main(String args[]) { IMessage msg = new MessageImpl() ; System.out.println(msg.getInfo()); } }
P195
P195
P195
P195
P195
P195
P195