由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - abstract class 的简单例子
相关主题
看了zhaoce073大水忍不住说2句Re: abstract classes GRAPHICS
Re: How to use abstract class?菜鸟问题
An interesting thing about java generics-do not laugh at me if u think it too basicQuestion on classpath
Re: 初级问题请问protected的使用
Re: 如何在两个窗口之间通信?Why String class is final?
泛型问题[合集] 谁能解释一下这里的protected specifier
请问有没有generic的array请教一个简单的问题
Is it possible to get Class object for T from a generic class? (下列空档,是否可填)问个题
相关话题的讨论汇总
话题: abstract话题: class话题: string话题: subclasses话题: sing
进入Java版参与讨论
1 (共1页)
u****s
发帖数: 2186
1
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.
s******e
发帖数: 493
2
template pattern.
see, GOF is so powerful. I only need type two words.
z*******3
发帖数: 13709
3
这叫简单吗?
等你把这一堆念完
半个小时过去了
还不包括人家问你问题的时间
而且你没发现这是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.

z*******3
发帖数: 13709
4
PDF/HTML/Excel/CSV
这个例子是有问题的,这才四个,如果是40个呢?
我现在处理的特殊情况是1000多个,每一个都有自己的schema和特殊的tag
我要是不把格式给先标准化存在xml
再通过template和xslt去转成其他的xml等特殊格式
按你们这种写法,我会把自己累死
1000多个方法的硬编码,加上各种调试
测试那个小日本估计也会累死
我用现在的方法,今天一天我写了500多个
测试一次性通过,巨爽,顺便下班时候跟小日本聊了聊av和钓鱼岛问题
估计明天可以搞定
z*******3
发帖数: 13709
5
很多人脑子就是转不过弯来
想想后来为什么会有jsp这种东西
先有servlet再有jsp的
但是你用servlet去写response?
累死你
有了jsp就简单多了
实际上xsl就是起到一个jsp的作用
只不过最终这个东东生产的是xml而jsp最终生产的是html而已
无非就是模板化最终的输出
我相信说到这里还是有人不太理解
肯定会有人还会继续举出各种animal的例子
然后不停地强调,这个方法可行,可行啊可行,我就是这么做的……
另外,pdf之类的,我们也是先做好一个没有填充的pdf模板
然后再通过itext读入以后做数据填充以后再输出
本质上跟jsp还有xsl是一样的
最后就是,xml的解析是core java的东西
dom和sax都是java的标准库,所以这不是什么高级或者是你找不到的东西
就是android里面都有
a****i
发帖数: 1182
6
标准化实际上就是虚类的一部分
怎么就没用了?

【在 z*******3 的大作中提到】
: PDF/HTML/Excel/CSV
: 这个例子是有问题的,这才四个,如果是40个呢?
: 我现在处理的特殊情况是1000多个,每一个都有自己的schema和特殊的tag
: 我要是不把格式给先标准化存在xml
: 再通过template和xslt去转成其他的xml等特殊格式
: 按你们这种写法,我会把自己累死
: 1000多个方法的硬编码,加上各种调试
: 测试那个小日本估计也会累死
: 我用现在的方法,今天一天我写了500多个
: 测试一次性通过,巨爽,顺便下班时候跟小日本聊了聊av和钓鱼岛问题

1 (共1页)
进入Java版参与讨论
相关主题
问个题Re: 如何在两个窗口之间通信?
初学者try catch in constructor 问题泛型问题
三论abstract class请问有没有generic的array
问两个语法问题Is it possible to get Class object for T from a generic class? (下列空档,是否可填)
看了zhaoce073大水忍不住说2句Re: abstract classes GRAPHICS
Re: How to use abstract class?菜鸟问题
An interesting thing about java generics-do not laugh at me if u think it too basicQuestion on classpath
Re: 初级问题请问protected的使用
相关话题的讨论汇总
话题: abstract话题: class话题: string话题: subclasses话题: sing