==Usage on Class
package generic.test;
public class Point<T extends Number> {
private T x;
private T y;
public Point(T x, T y) {
super();
this.x = x;
this.y = y;
}
public String getInfo() {
return "x= " + x + ", y = " + y;
}
}
package generic.test;
public class TestPoint {
public static void main(String[] args) {
Point<Integer> p1 = new Point<Integer>(10, 20);
Point<Float> p2 = new Point<Float>(20.0f, 30.0f);
print(p1);
print(p2);
}
public static void print(Point<?> p){
System.out.println(p.getInfo());
}
}
----java output----
x= 10, y = 20
x= 20.0, y = 30.0
Note: extends defines the upper limits, supper defines the lower limits
==Usage on Interface
package generic.test;
public interface draw<T> {
void start(T p);
void end(T p);
}
class Point<T> implements draw<T> {
public void start (T p) {
System.out.println("point start" + p);
}
public void end (T p) {
System.out.println("point end" + p);
}
}
class Line implements draw<Line> {
public void start(Line p) {
System.out.println("line start" + p);
}
public void end(Line p) {
System.out.println("line ends" + p);
}
}
package generic.test;
public class TInterface {
public static void main(String[] args) {
Point<Integer> p = new Point<Integer>();
Line l = new Line();
p.start(0);
p.end(10);
l.start(l);
l.end(l);
}
}
----java output----
point start0
point end10
line startgeneric.test.Line@e2cb55
line endsgeneric.test.Line@e2cb55
==Usage on Method
public class Tmethod {
public static void main(String[] args) {
for(int i:teven(1,2,3)) {
System.out.println(i);
}
}
public static <T> T[] teven(T... tn) {
return tn;
}
}
----java output----
1
2
3
==Recursive Usage
package generic.test;
class Info<T> {
private T param;
public T getParam() {
return param;
}
public void setParam(T param) {
this.param = param;
}
}
class Person<T> {
private T info;
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
}
public class Trecursive {
public static void main(String[] args) {
Person<Info<String>> per = new Person<Info<String>>();
per.setInfo(new Info<String>());
per.getInfo().setParam("mldnjava");
System.out.println(per.getInfo().getParam());
}
}
----javaoutput----
mldnjava
2011年12月31日星期六
第10天第3节: 包、访问控制权限
1. package definition and import
a. definition: package org.tx.msdn
b. import: import org.tx.msdn.*
c. refer: Person per = new org.tx.msdn Person(); or Person per=new Person() if not confict
d. import static: import static org.tx.msdn.Math;
if a class contains only static method, using "import static" can assume the method defined is in the current class. ( which means use the mehtod name directly without the class name)
2. compile
javac -d . Person.java ==> creates ./org/tx/msdn/Person.class
java org/tx/msdn/Person
3. jar: same as a directory
jar -cvf my.jar org
set classpath=.;my.jar
4. access control of class
public: all
protected: class from same package, derived class from different package
default: class from same package
private: only current class
5. common package
a. java.lang => implicitly import, contains String...
b. java.lang.refect ==> low level system
c. java.util ==> tool
d. java.io ==> I/O
e. java.net ==> netowrk
f. java.sql ==> database
g. java.txt ==> internationlaztion
a. definition: package org.tx.msdn
b. import: import org.tx.msdn.*
c. refer: Person per = new org.tx.msdn Person(); or Person per=new Person() if not confict
d. import static: import static org.tx.msdn.Math;
if a class contains only static method, using "import static" can assume the method defined is in the current class. ( which means use the mehtod name directly without the class name)
2. compile
javac -d . Person.java ==> creates ./org/tx/msdn/Person.class
java org/tx/msdn/Person
3. jar: same as a directory
jar -cvf my.jar org
set classpath=.;my.jar
4. access control of class
public: all
protected: class from same package, derived class from different package
default: class from same package
private: only current class
5. common package
a. java.lang => implicitly import, contains String...
b. java.lang.refect ==> low level system
c. java.util ==> tool
d. java.io ==> I/O
e. java.net ==> netowrk
f. java.sql ==> database
g. java.txt ==> internationlaztion
第10天第1节: 异常的捕获及处理
===Exception
1. Syntax
try {....
throw Exception;
}catch(Exception_Type1 e){
...
}catch(Exception_Type2 e){
...
}catch(Exception_Type3 e){
...
}finally{
...
}
2. class Exception
class Throwable ->class Exception ==> program error
class Throwable->class Exception ->class RuntimeException
class Throwable->class Error ==>JVM error
Exception(String s)
3. Throws: don't handle exception in current method
public void div(int i, int j) throws Exception:
5. Exception must be caught except for RuntimeException
1. Syntax
try {....
throw Exception;
}catch(Exception_Type1 e){
...
}catch(Exception_Type2 e){
...
}catch(Exception_Type3 e){
...
}finally{
...
}
2. class Exception
class Throwable ->class Exception ==> program error
class Throwable->class Exception ->class RuntimeException
class Throwable->class Error ==>JVM error
Exception(String s)
3. Throws: don't handle exception in current method
public void div(int i, int j) throws Exception:
5. Exception must be caught except for RuntimeException
2011年12月30日星期五
第10天第1节: 匿名内部类、包装类
==object 类
object类是所有类的父类。
1. public boolean equals(object obj) : comparing if two object content are same or not
2. public String toString() : what to be printed int println
3. public int hashCode() : get the hash code
所有类都可以override这些方法。
==包装类
8种基本类型不是类,但是java要求一切都是类。所有8种基本类型有对应的包装和解包装方法
1. based on class Object
Boolean
Character
2. based on class Number
Method
Byte byteValue()
Float floatValue()
Double doubleValue()
Integer integerValue()
Short shortValue()
Long longValue()
3. example:
int i = 30;
Integer i = Integer(i);
int j = i.intValue();
After JDK1.5
Integer i =30;
int j = i;
4. application
Integer: public static int parseInt(String s);
Float: public static float parseFloat(String s);
object类是所有类的父类。
1. public boolean equals(object obj) : comparing if two object content are same or not
2. public String toString() : what to be printed int println
3. public int hashCode() : get the hash code
所有类都可以override这些方法。
==包装类
8种基本类型不是类,但是java要求一切都是类。所有8种基本类型有对应的包装和解包装方法
1. based on class Object
Boolean
Character
2. based on class Number
Method
Byte byteValue()
Float floatValue()
Double doubleValue()
Integer integerValue()
Short shortValue()
Long longValue()
3. example:
int i = 30;
Integer i = Integer(i);
int j = i.intValue();
After JDK1.5
Integer i =30;
int j = i;
4. application
Integer: public static int parseInt(String s);
Float: public static float parseFloat(String s);
第9天第5节: 抽象类、接口的实际作用
1. abstract vs interface
2. 模板设计方法(抽象类)
父类为抽象类:定义若干抽象方法,定义一个正常的模板方法,该模板方法调用抽象方法返回值
子类为正常类:实现抽象方法并放回值
主函数类: 创建子类并调用抽象类的模板方法
适合地方: Web开发
abstract class Product
{
public abstract String getName();
public abstract float getPrice();
public String getInfo() {
return "Name: " + this.getName() + "Price: " + this.getPrice();
}
}
class Dog extends Product
{
public String getName() {
return "dog";
}
public float getPrice() {
return 15.0f;
}
}
class Cat extends Product
{
public String getName() {
return "cat";
}
public float getPrice() {
return 25.0f;
}
}
public class testb
{
public static void main ( String args[] ) {
Product d = new Dog();
Product c = new Cat();
System.out.println(d.getInfo());
System.out.println(c.getInfo());
}
}
---------- java ----------
Name: dogPrice: 15.0
Name: catPrice: 25.0
2. 制定标准设计方法(接口类)
父类为接口类:定义若干抽象方法
子类为正常类:实现抽象方法
第三方类: 定义正常方法,该方法使用接口类参数
主函数类: 创建第三方类,并多次调用第三方类的方法,每次调用给以不用的子类为参数
适合地方:接口标准的制定,例如USB标准 interface usb
{
void start();
void end();
}
class printer implements usb
{
public void start () {
System.out.println("printer starts");
}
public void end () {
System.out.println("printer ends");
}
}
class flash implements usb
{
public void start () {
System.out.println("flash starts");
}
public void end () {
System.out.println("flash ends");
}
}
class computer
{
void plugin(usb u) {
u.start();
u.end();
}
}
public class testb
{
public static void main ( String args[] ) {
computer com = new computer();
com.plugin(new flash());
com.plugin(new printer());
}
}
---------- java ----------
flash starts
flash ends
printer starts
printer ends
2.工厂设计模式
父类为接口类:定义若干抽象方法
子类为正常类:实现抽象方法
工厂类: 定义正常方法,该方法根据String类常数放回所创建的子对象的接口类句柄
主函数类: 从工厂获得子对象,并调用该子对象的抽象方法
适合地方:Java开发常用,客户只需了解接口和产品信息描述即可
interface usb
{
void start();
void end();
}
class printer implements usb
{
public void start () {
System.out.println("printer starts");
}
public void end () {
System.out.println("printer ends");
}
}
class flash implements usb
{
public void start () {
System.out.println("flash starts");
}
public void end () {
System.out.println("flash ends");
}
}
class factory
{
public usb getInstance(String name) {
usb f=null;
if ( name.equals("flash")) {
f = new flash();
}else if ( name.equals("printer"))
{
f = new printer();
}
return f;
}
}
public class testb
{
public static void main ( String args[] ) {
factory fac = new factory();
usb f = fac.getInstance("printer");
f.start();
f.end();
}
}
---------- java ----------
printer starts
printer ends
==代理设计模式
设计模式中定义: 为其他对象提供一种代理以控制对这个对象的访问.
为什么要使用Proxy?
1.授权机制 不同级别的用户对同一对象拥有不同的访问权利
2.某个客户端不能直接操作到某个对象,但又必须和那个对象有所互动.
(1)如果那个对象是一个是很大的图片,需要花费很长时间才能显示出来,那么当这个图片包含在文档中时,使用编辑器或浏览器打开这个文档,打开文档必须很迅速,不能等待大图片处理完成,这时需要做个图片Proxy来代替真正的图片.
(2)如果那个对象在Internet的某个远端服务器上,直接操作这个对象因为网络速度原因可能比较慢,那我们可以先用Proxy来代替那个对象.
==适配器设计模式
接口类: 定义了所有可能要在子类中出现的抽象方法
抽象类: dummy实现了所有接口类的方法
子类:extends抽象类并真正实现它自己说关心的方法
2011年12月29日星期四
第9天第4节: 接口、对象多态性
===Interface
-A interface is a class which contains only globle constant and abstract methods
Example:
interface A {
public static final String NAME="TRYME"
public abstract void printA();
}
-Where "public static final" and "public abstract" could be ommited
Example:
interface A {
String NAME="TRYME";
void printA();
}
interface B {
String SUB="ME";
void printB();
}
-Interface with normal class: interface must has iclass ( or interface and finally normal class)
-derived class must override all abstract method
-A derived class can inherit from multiple interfaces
class X implements A,B {
public void printA() {
System.out.println( "hello");
}
public void printB() {
System.out.println("world");
}
}
-derived class can inherit from both abstract class and implements interfaces
public abstract class C {
public abstract void printC();
}
public class X extends C implements A,B {
public void printA() {
System.out.println( "hello");
}
public void printB() {
System.out.println("world");
}
public void printC() {
System.out.println("!!!");
}
}
-abstract class can implements interface
-no methods implementation needed
abstract class D implements A {
public abstract void printD() {
System.out.println("DDD");
}
}
class X externd D {
public void printA () {.....}
public void printD () {.....}
}
-Interface inherits Interface
-use "extends"
-must implments all mothds
interface C extends A,B {
public void printC();
}
class X implements C {
public void printA() {....}
public void printB() {....}
public void printC() {...}
}
==Polymorphism
public class A { public void fun1() {...}}
public class B extends A { public void fun1() {...}
B b = new B();
A a = b;
a.fun1() ==> calls B->fun1()
--note object a can only use method defined in class A, it calls B's method if it is overridden;
--automatically casting
A a = new B();
B b = (B) a;
a.fun1() ==> calls B->fun1()
--note object b can use method defined in class B, it calls B's method if it is overridden;
--the original object must defined as class B
A a = new A();
B b = (B) a;
--Compilation Error: because A don't knows B's all field and methods
-Example: define a method with can get a argument of any derived class from A
public class Demo {
public static void main(String args[] ) {
fun(new A());
fun(new B());
}
public static void fun(A a) {
a.fun1();
}
==instanceof
instanceof is used to determine if an object a given class.
class B extends A
A a = new A();
A b = new B();
a instanceof A ==> true, a instanceof B ==> faluse
b instanceof A ==> true, b instanceof B ==>true
==rule?
let a class inherit only from abstract class or interface, not from a normal class?
第9天第3节: final关键字、抽象类
==Final
final on class: this class can not be inheritanced
final on a method: this method can not be overriding
final on a field: this is a constant. usually the name is capitalized
public static final: a global constant shared by all objects
==Abstract class
abstract class: a class contains any abstract method
abstract method: a dummy method has declaration without implementation
Example:
public abstract class A {
public String name = "hello";
public abstract void print();
}
abstract class must be extended, all abstract method must has implementation
final on class: this class can not be inheritanced
final on a method: this method can not be overriding
final on a field: this is a constant. usually the name is capitalized
public static final: a global constant shared by all objects
==Abstract class
abstract class: a class contains any abstract method
abstract method: a dummy method has declaration without implementation
Example:
public abstract class A {
public String name = "hello";
public abstract void print();
}
abstract class must be extended, all abstract method must has implementation
第9天第2节: 两个重要比较
1. overloading vs overriding
Definition:
overloading: same method name, different number of argument or argument type. no permission limitation
overriding: same method name, number of argument and argument type. private<default<public
Usage:
overloading; within a class
overriding: in a class inheritance
2. this vs super
.member or .method
this: looking in current class first, then looking in parent class if not found in current class
super: looking in the parent class
this() or supper() used in the constructor
this(): call constructor of current class, put at the first line
supper(): call constructor of parent class, put at the first line
refer to a object ( passed in as argument)
this: means current object
supper: no such concept
第9天第1节: 继承的基本概念
==Syntax
class Base {
....
}
class Drived extends Base {
....
}
==Usage:
-adding new properties and methods
-overriding base properties and mehtods
==Note:
-no-argument construct of base class is called automatically, can called explicitly by super();
-has-argument constract of base class must be called explicitly at the first line of constructor of derived class
-derived class can extends only one base class
-derived class can only access public member and method of base class
class Base {
....
}
class Drived extends Base {
....
}
==Usage:
-adding new properties and methods
-overriding base properties and mehtods
==Note:
-no-argument construct of base class is called automatically, can called explicitly by super();
-has-argument constract of base class must be called explicitly at the first line of constructor of derived class
-derived class can extends only one base class
-derived class can only access public member and method of base class
2011年12月28日星期三
第8天第1节: 内部类
==Use as two classes
class Outter
{
String name="outter";
String oname="oname";
public class Innter
{
String name="inner";
String iname="iname";
}
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.oname);
//System.out.println(o1.Innter.iname); fails because Innter don't have instance
//Innter i1 = new Innter(); fails because this is a inner class
//Innter i1 = new Outter.Inner(); fails because Inner is not a member.
Outter.Innter i1 = o1.new Innter();
System.out.println(i1.iname);
}
}
---------- java ----------
oname
iname
==Advantage: Inner class can access Outter class menber
class Outter
{
String name="outter";
String oname="oname";
public class Innter
{
String name="inner";
String iname="iname";
public String getInfo() {
return "oname from Innter = " + oname;
}
}
// correct usage
public String getInfo() {
Innter i1 = new Innter();
return i1.name + "\n" + i1.getInfo();
}
/* incorrect usage because no Inner instance
public String getInfo() {
return Innter.name;
}
*/
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.getInfo());
}
}
---------- java ----------
inner
oname from Innter = oname
==Static inner class and access
class Outter
{
String name="outter";
String oname="oname";
public static class Innter
{
static String name="inner";
static String iname="iname";
}
// correct usage
public String getInfo() {
return Innter.name; //access type 1
}
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.getInfo());
System.out.println(Outter.Innter.name); //access type2
// System.out.println(o1.Inter.name); failure Inter is not a class member.
}
}
class Outter
{
String name="outter";
String oname="oname";
public class Innter
{
String name="inner";
String iname="iname";
}
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.oname);
//System.out.println(o1.Innter.iname); fails because Innter don't have instance
//Innter i1 = new Innter(); fails because this is a inner class
//Innter i1 = new Outter.Inner(); fails because Inner is not a member.
Outter.Innter i1 = o1.new Innter();
System.out.println(i1.iname);
}
}
---------- java ----------
oname
iname
==Advantage: Inner class can access Outter class menber
class Outter
{
String name="outter";
String oname="oname";
public class Innter
{
String name="inner";
String iname="iname";
public String getInfo() {
return "oname from Innter = " + oname;
}
}
// correct usage
public String getInfo() {
Innter i1 = new Innter();
return i1.name + "\n" + i1.getInfo();
}
/* incorrect usage because no Inner instance
public String getInfo() {
return Innter.name;
}
*/
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.getInfo());
}
}
---------- java ----------
inner
oname from Innter = oname
==Static inner class and access
class Outter
{
String name="outter";
String oname="oname";
public static class Innter
{
static String name="inner";
static String iname="iname";
}
// correct usage
public String getInfo() {
return Innter.name; //access type 1
}
}
public class testb
{
public static void main ( String args[] ) {
Outter o1 = new Outter();
System.out.println(o1.getInfo());
System.out.println(Outter.Innter.name); //access type2
// System.out.println(o1.Inter.name); failure Inter is not a class member.
}
}
第7天第5节: 对象数组、构造方法私有化程序分析
==代码块
主方法静态代码块优先于主方法,
在普通类中静态块优先于构造块,
在普通类中构造块优先于构造方法,
静态块只实例化一次。 ......
1. 普通代码块, why the following is differnt?
**fail
public static void main ( String args[] ) {
int x = 50;
System.out.println("x= "+x);
{
int x= 30; //fail: x is already defined in main
System.out.println("x= " + x);
}
}
**pass
public static void main ( String args[] ) {
{
int x= 30;
System.out.println("x= " + x);
}
int x = 50;
System.out.println("x= "+x);
}
2. 构造块
class Demo
{
String name="aa";
{
System.out.println(name); //==>always run before Demo()
}
}
2. 静态块
class Demo
{
String name="aa";
static String sname="saa";
static {
System.out.println("static kuai " + sname);
}
{
System.out.println("gou zhao kuai " + name);
}
public Demo() {
System.out.println("gou zhao function " + name);
}
}
public class testb
{
public static void main ( String args[] ) {
new Demo();
System.out.println();
new Demo();
}
}
---------- java output----------
static kuai saa
gou zhao kuai aa
gou zhao function aa
gou zhao kuai aa
gou zhao function aa
===构造方法私有化
1. Purpose: Let only one instance of a class in an application
2. example
class Demo
{
String name = "aa";
static Demo instance = new Demo();
private Demo() {
}
public void print() {
System.out.println(name);
}
public static Demo getInstance() {
return instance;
}
}
public class testb
{
public static void main ( String args[] ) {
Demo d0 = Demo.instance; // this one if instance is public
Demo d1 = Demo.getInstance(); // this one if instance is private
System.out.print(d1+"\t");
d1.print();
System.out.print(d1.getInstance()+"\t");
d1.getInstance().print();
System.out.print(d1.getInstance().getInstance()+"\t");
d1.getInstance().getInstance().print();
}
}
---------- java ----------
Demo@497934 aa
Demo@497934 aa
Demo@497934 aa
====对象数组
class Demo
{
String name = "aa";
Demo ( String name ) {
this.name = name;
}
}
public class testb
{
public static void main ( String args[] ) {
Demo d1[] = new Demo[2]; //dynamic initialization
d1[0] = new Demo("a0");
d1[1] = new Demo("a1");
for(Demo x:d1){
System.out.println(x.name);
}
Demo d2[] = { new Demo("a3"), new Demo("a4") }; //static initialization
for(Demo x:d2){
System.out.println(x.name);
}
}
}
---------- java ----------
a0
a1
a3
a4
主方法静态代码块优先于主方法,
在普通类中静态块优先于构造块,
在普通类中构造块优先于构造方法,
静态块只实例化一次。 ......
1. 普通代码块, why the following is differnt?
**fail
public static void main ( String args[] ) {
int x = 50;
System.out.println("x= "+x);
{
int x= 30; //fail: x is already defined in main
System.out.println("x= " + x);
}
}
**pass
public static void main ( String args[] ) {
{
int x= 30;
System.out.println("x= " + x);
}
int x = 50;
System.out.println("x= "+x);
}
2. 构造块
class Demo
{
String name="aa";
{
System.out.println(name); //==>always run before Demo()
}
}
2. 静态块
class Demo
{
String name="aa";
static String sname="saa";
static {
System.out.println("static kuai " + sname);
}
{
System.out.println("gou zhao kuai " + name);
}
public Demo() {
System.out.println("gou zhao function " + name);
}
}
public class testb
{
public static void main ( String args[] ) {
new Demo();
System.out.println();
new Demo();
}
}
---------- java output----------
static kuai saa
gou zhao kuai aa
gou zhao function aa
gou zhao kuai aa
gou zhao function aa
===构造方法私有化
1. Purpose: Let only one instance of a class in an application
2. example
class Demo
{
String name = "aa";
static Demo instance = new Demo();
private Demo() {
}
public void print() {
System.out.println(name);
}
public static Demo getInstance() {
return instance;
}
}
public class testb
{
public static void main ( String args[] ) {
Demo d0 = Demo.instance; // this one if instance is public
Demo d1 = Demo.getInstance(); // this one if instance is private
System.out.print(d1+"\t");
d1.print();
System.out.print(d1.getInstance()+"\t");
d1.getInstance().print();
System.out.print(d1.getInstance().getInstance()+"\t");
d1.getInstance().getInstance().print();
}
}
---------- java ----------
Demo@497934 aa
Demo@497934 aa
Demo@497934 aa
====对象数组
class Demo
{
String name = "aa";
Demo ( String name ) {
this.name = name;
}
}
public class testb
{
public static void main ( String args[] ) {
Demo d1[] = new Demo[2]; //dynamic initialization
d1[0] = new Demo("a0");
d1[1] = new Demo("a1");
for(Demo x:d1){
System.out.println(x.name);
}
Demo d2[] = { new Demo("a3"), new Demo("a4") }; //static initialization
for(Demo x:d2){
System.out.println(x.name);
}
}
}
---------- java ----------
a0
a1
a3
a4
2011年12月26日星期一
订阅:
博文 (Atom)