u*s
2 楼
When & why abstract class:
1. All subclasses of the abstract class inherit from the abstract class
There is an 'Is-A' relationship between abstract class and its subclasses
. Interface is a 'Can-Do' relationship.
2. All subclasses share most of the fields and methods, only one or just a
few different methods with different implementation. If all these classes
implement an interface, you just put it in the abstract class declaration.
3. Sometimes you only want these methods mentioned in 2. to be different and
keep other logic in abstract class and you can change it only at one place.
You can make these methods private if you don't want subclasses change them.
4. When you have a new type of such class, you just extends the abstract
class and implement the abstract methods.
A simple example:
interface Fun { public void havingFun(); }
public abstract class Animal implements Fun {
protected String eat() {
return "Eat.";
}
protected String drink() {
return "Drink.";
}
protected abstract String sing();
public void havingFun() {
System.out.println(eatDrinkSing());
}
private String eatDrinkSing() {
return eat() + drink() + sing();
}
}
class Bird extends Animal {
protected String sing() {
return "Chirp.";
}
}
class Dog extends Animal {
protected String sing() {
return "Bark.";
}
}
when you need to create another class, just extend the abstract class Animal
,
class Cat extends Animal {
protected String sing() {
return "Meow.";
}
}
Note:
The HavingFun interface is implemented in the super class(the abstract class
), it uses the abstract method sing(). The actual implementation of sing()
is in the subclasses.
If you decided to change is to sing()+drink()+eat() (different order), you
just change it at one place, not in every of the subclasses.
I worked on an almost exact PDF/HTML/Excel/CSV exporter as SleeplessNYC
mention. Yes, abstract class is used. I used AbstractAction a lot when
working with Swing.
1. All subclasses of the abstract class inherit from the abstract class
There is an 'Is-A' relationship between abstract class and its subclasses
. Interface is a 'Can-Do' relationship.
2. All subclasses share most of the fields and methods, only one or just a
few different methods with different implementation. If all these classes
implement an interface, you just put it in the abstract class declaration.
3. Sometimes you only want these methods mentioned in 2. to be different and
keep other logic in abstract class and you can change it only at one place.
You can make these methods private if you don't want subclasses change them.
4. When you have a new type of such class, you just extends the abstract
class and implement the abstract methods.
A simple example:
interface Fun { public void havingFun(); }
public abstract class Animal implements Fun {
protected String eat() {
return "Eat.";
}
protected String drink() {
return "Drink.";
}
protected abstract String sing();
public void havingFun() {
System.out.println(eatDrinkSing());
}
private String eatDrinkSing() {
return eat() + drink() + sing();
}
}
class Bird extends Animal {
protected String sing() {
return "Chirp.";
}
}
class Dog extends Animal {
protected String sing() {
return "Bark.";
}
}
when you need to create another class, just extend the abstract class Animal
,
class Cat extends Animal {
protected String sing() {
return "Meow.";
}
}
Note:
The HavingFun interface is implemented in the super class(the abstract class
), it uses the abstract method sing(). The actual implementation of sing()
is in the subclasses.
If you decided to change is to sing()+drink()+eat() (different order), you
just change it at one place, not in every of the subclasses.
I worked on an almost exact PDF/HTML/Excel/CSV exporter as SleeplessNYC
mention. Yes, abstract class is used. I used AbstractAction a lot when
working with Swing.
D*l
3 楼
哈哈
s*e
4 楼
template pattern.
see, GOF is so powerful. I only need type two words.
see, GOF is so powerful. I only need type two words.
z*3
6 楼
这叫简单吗?
等你把这一堆念完
半个小时过去了
还不包括人家问你问题的时间
而且你没发现这是desktop的东西吗?
j2ee中压根木有这种东西
为什么呢?因为有fk这种东西存在
animal和cat是存在两个不同表里面的实体数据
而很自然的类对应关系就是
class cat{
private animalId;
public setter/getter method;
}
gof的很多模式都是desktop的东西
书呆子的玩意,很多人都不写了
不过我在写……
subclasses
and
place.
them.
【在 u****s 的大作中提到】
: When & why abstract class:
: 1. All subclasses of the abstract class inherit from the abstract class
: There is an 'Is-A' relationship between abstract class and its subclasses
: . Interface is a 'Can-Do' relationship.
: 2. All subclasses share most of the fields and methods, only one or just a
: few different methods with different implementation. If all these classes
: implement an interface, you just put it in the abstract class declaration.
: 3. Sometimes you only want these methods mentioned in 2. to be different and
: keep other logic in abstract class and you can change it only at one place.
: You can make these methods private if you don't want subclasses change them.
等你把这一堆念完
半个小时过去了
还不包括人家问你问题的时间
而且你没发现这是desktop的东西吗?
j2ee中压根木有这种东西
为什么呢?因为有fk这种东西存在
animal和cat是存在两个不同表里面的实体数据
而很自然的类对应关系就是
class cat{
private animalId;
public setter/getter method;
}
gof的很多模式都是desktop的东西
书呆子的玩意,很多人都不写了
不过我在写……
subclasses
and
place.
them.
【在 u****s 的大作中提到】
: When & why abstract class:
: 1. All subclasses of the abstract class inherit from the abstract class
: There is an 'Is-A' relationship between abstract class and its subclasses
: . Interface is a 'Can-Do' relationship.
: 2. All subclasses share most of the fields and methods, only one or just a
: few different methods with different implementation. If all these classes
: implement an interface, you just put it in the abstract class declaration.
: 3. Sometimes you only want these methods mentioned in 2. to be different and
: keep other logic in abstract class and you can change it only at one place.
: You can make these methods private if you don't want subclasses change them.
h*2
7 楼
路过
换衣服了 求打分!
感觉这贴要火~~~
换衣服了 求打分!
感觉这贴要火~~~
z*3
8 楼
PDF/HTML/Excel/CSV
这个例子是有问题的,这才四个,如果是40个呢?
我现在处理的特殊情况是1000多个,每一个都有自己的schema和特殊的tag
我要是不把格式给先标准化存在xml
再通过template和xslt去转成其他的xml等特殊格式
按你们这种写法,我会把自己累死
1000多个方法的硬编码,加上各种调试
测试那个小日本估计也会累死
我用现在的方法,今天一天我写了500多个
测试一次性通过,巨爽,顺便下班时候跟小日本聊了聊av和钓鱼岛问题
估计明天可以搞定
这个例子是有问题的,这才四个,如果是40个呢?
我现在处理的特殊情况是1000多个,每一个都有自己的schema和特殊的tag
我要是不把格式给先标准化存在xml
再通过template和xslt去转成其他的xml等特殊格式
按你们这种写法,我会把自己累死
1000多个方法的硬编码,加上各种调试
测试那个小日本估计也会累死
我用现在的方法,今天一天我写了500多个
测试一次性通过,巨爽,顺便下班时候跟小日本聊了聊av和钓鱼岛问题
估计明天可以搞定
z*3
10 楼
很多人脑子就是转不过弯来
想想后来为什么会有jsp这种东西
先有servlet再有jsp的
但是你用servlet去写response?
累死你
有了jsp就简单多了
实际上xsl就是起到一个jsp的作用
只不过最终这个东东生产的是xml而jsp最终生产的是html而已
无非就是模板化最终的输出
我相信说到这里还是有人不太理解
肯定会有人还会继续举出各种animal的例子
然后不停地强调,这个方法可行,可行啊可行,我就是这么做的……
另外,pdf之类的,我们也是先做好一个没有填充的pdf模板
然后再通过itext读入以后做数据填充以后再输出
本质上跟jsp还有xsl是一样的
最后就是,xml的解析是core java的东西
dom和sax都是java的标准库,所以这不是什么高级或者是你找不到的东西
就是android里面都有
想想后来为什么会有jsp这种东西
先有servlet再有jsp的
但是你用servlet去写response?
累死你
有了jsp就简单多了
实际上xsl就是起到一个jsp的作用
只不过最终这个东东生产的是xml而jsp最终生产的是html而已
无非就是模板化最终的输出
我相信说到这里还是有人不太理解
肯定会有人还会继续举出各种animal的例子
然后不停地强调,这个方法可行,可行啊可行,我就是这么做的……
另外,pdf之类的,我们也是先做好一个没有填充的pdf模板
然后再通过itext读入以后做数据填充以后再输出
本质上跟jsp还有xsl是一样的
最后就是,xml的解析是core java的东西
dom和sax都是java的标准库,所以这不是什么高级或者是你找不到的东西
就是android里面都有
相关阅读
JBuilder 5 EE downloadanswer Re: how HashMap/Hashtable compare key?Re: How to write to a file on server in an applet?Core Java2 Notes (2)Re: Pls recommand a Java book for beginner!Java版BBS之设想Brainbench ejb examRe: [转载] 如何看某个端口是否被占用?(use UNIX command or ..)Re: Is there a free IDE with code autocompletion?Re: 求JAVA简历Re: Any good java profiler tools?Please give me some ideas!! About the Object database invocation.Re: about promotion (question from novice)Java Telnet Client Implementation v0.991BetaJ2EE vs .NET (上)(z)Re: 请问过来人:(ZZ) J2EE vs. Microsoft.NETstatic vs. final[公告] Java 板的投票结果UNIX build using ANT!!!