总览:
抽象类与接口
1,抽象类
demo_抽象类
abstract class 抽象父类
{
private String say;
abstract public void print ( ) ;
public void setSay ( String say) {
this . say= say;
}
public String getSay ( ) {
return this . say;
}
}
class 子类 extends 抽象父类
{
public void print ( ) {
System. out. println ( "子类覆写抽象类方法" ) ;
}
}
public class 抽象类
{
public static void main ( String agrs[ ] ) {
抽象父类 a= new 子类( ) ;
a. setSay ( "利用子类设置抽象类属性" ) ;
System. out. println ( a. getSay ( ) ) ;
a. print ( ) ;
}
}
demo_模板设计模式
abstract class Action
{
public static final int EAT= 1 ;
public final static int REST= 5 ;
public final static int WORK= 10 ;
public void actionSwitch ( int code) {
switch ( code) {
case EAT: {
this . eat ( ) ;
break ;
}
case REST: {
this . rest ( ) ;
break ;
}
case WORK: {
this . work ( ) ;
break ;
}
}
}
abstract public void eat ( ) ;
abstract public void rest ( ) ;
abstract public void work ( ) ;
}
class Robot extends Action
{
public void eat ( ) { }
public void rest ( ) { }
public void work ( ) {
System. out. println ( "机器人重复性的工作" ) ;
}
}
class Person extends Action
{
public void eat ( ) {
System. out. println ( "人优雅的吃饭" ) ;
}
public void rest ( ) {
System. out. println ( "人在床上睡觉" ) ;
}
public void work ( ) {
System. out. println ( "人勤奋的工作" ) ;
}
}
class Pig extends Action
{
public void eat ( ) {
System. out. println ( "猪狼吞虎咽的吃饭" ) ;
}
public void rest ( ) {
System. out. println ( "猪席地而睡" ) ;
}
public void work ( ) { }
}
public class 抽象类_模板设计模式
{
public static void main ( String agrs[ ] ) {
Action per= new Person ( ) ;
Action rob= new Robot ( ) ;
Action pig= new Pig ( ) ;
System. out. println ( "--------------------人-----------------" ) ;
per. actionSwitch ( 1 ) ;
per. actionSwitch ( 5 ) ;
per. actionSwitch ( 10 ) ;
System. out. println ( "------------------机器人-----------------" ) ;
rob. actionSwitch ( 1 ) ;
rob. actionSwitch ( 5 ) ;
rob. actionSwitch ( 10 ) ;
System. out. println ( "--------------------猪-----------------" ) ;
pig. actionSwitch ( 1 ) ;
pig. actionSwitch ( 5 ) ;
pig. actionSwitch ( 10 ) ;
}
}
2,包装类
demo_包装类
class Int
{
private int data;
public Int ( int data) {
this . data= data;
}
public int intValue ( ) {
return this . data;
}
}
public class 包装类{
public static void main ( String agrs[ ] ) {
Object obj= new Int ( 10 ) ;
System. out. println ( ( ( Int) obj) . intValue ( ) ) ;
Integer data= new Integer ( 10 ) ;
int num= data. intValue ( ) ;
System. out. println ( num) ;
Integer a= 10 ;
int x= ++ a;
a-- ;
System. out. println ( x) ;
System. out. println ( a. equals ( data) ) ;
}
}
3,接口
demo_接口
abstract class Welcome
{
public abstract void welcome ( ) ;
}
interface IMessage
{
public static final String INFO= "接口" ;
public abstract void getInfo ( ) ;
}
interface IChannel
{
public abstract boolean channelConnect ( ) ;
}
interface IDatabase
{
public abstract boolean databaseConnect ( ) ;
}
interface IService extends IChannel , IDatabase
{
public abstract String service ( ) ;
}
class MessageService extends Welcome implements IMessage , IService
{
public void welcome ( ) {
System. out. println ( "欢迎使用信息服务" ) ;
}
public boolean channelConnect ( ) {
return true ;
}
public boolean databaseConnect ( ) {
return true ;
}
public String service ( ) {
return "服务连接成功" ;
}
public void getInfo ( ) {
this . welcome ( ) ;
if ( this . channelConnect ( ) ) {
if ( this . databaseConnect ( ) ) {
System. out. println ( this . service ( ) ) ;
} else {
System. out. println ( "数据库连接错误" ) ;
}
} else {
System. out. println ( "频道连接错误" ) ;
}
}
}
public class 接口
{
public static void main ( String agrs[ ] ) {
IMessage mess= new MessageService ( ) ;
mess. getInfo ( ) ;
}
}
demo_工厂设计模式
interface IEat
{
public void eat ( ) ;
}
class Bread implements IEat
{
public void eat ( ) {
System. out. println ( "吃面包" ) ;
}
}
class Milk implements IEat
{
public void eat ( ) {
System. out. println ( "喝牛奶" ) ;
}
}
class Factory
{
public static IEat factoryGet ( String food) {
if ( food. equals ( "bread" ) ) {
return new Bread ( ) ;
} else if ( food. equals ( "milk" ) ) {
return new Milk ( ) ;
}
return null;
}
}
public class 工厂设计模式
{
public static void main ( String agrs[ ] ) {
IEat food= Factory. factoryGet ( agrs[ 0 ] ) ;
food. eat ( ) ;
}
}
demo_代理设计模式
interface IEat
{
public void get ( ) ;
}
class EatReal implements IEat
{
public void get ( ) {
System. out. println ( "[核心主题]:吃到了饭" ) ;
}
}
class EatProxy implements IEat
{
private IEat eat;
public EatProxy ( IEat eat) {
this . eat= eat;
}
public void get ( ) {
this . prepare ( ) ;
this . eat. get ( ) ;
this . clean ( ) ;
}
public void prepare ( ) {
System. out. println ( "[代理主题]:准备食物" ) ;
}
public void clean ( ) {
System. out. println ( "[代理主题]:清理食物" ) ;
}
}
public class 代理设计模式
{
public static void main ( String agrs[ ] ) {
IEat eat= new EatProxy ( new EatReal ( ) ) ;
eat. get ( ) ;
}
}
demo_工厂设计模式与代理设计模式结合
interface IEat {
void eat ( ) ;
}
class Milk implements IEat {
@Override
public void eat ( ) {
System. out. println ( "喝牛奶" ) ;
}
}
class Bread implements IEat {
@Override
public void eat ( ) {
System. out. println ( "吃面包" ) ;
}
}
class EatProxy implements IEat {
IEat eat;
public EatProxy ( IEat eat) {
this . eat = eat;
}
@Override
public void eat ( ) {
this . prepare ( ) ;
eat. eat ( ) ;
this . end ( ) ;
}
public void prepare ( ) {
System. out. println ( "开始进食" ) ;
}
public void end ( ) {
System. out. println ( "结束进食" ) ;
}
}
class Factory {
private Factory ( ) { }
public static IEat factory ( String food) {
switch ( food) {
case "milk" :
return new EatProxy ( new Milk ( ) ) ;
case "bread" :
return new EatProxy ( new Bread ( ) ) ;
default : return null;
}
}
}
public class Demo {
public static void main ( String[ ] args) {
IEat eat= Factory. factory ( "milk" ) ;
eat. eat ( ) ;
}
}
4,泛型
demo_泛型问题的引出
class Point
{
private Object x;
private Object y;
public void setX ( Object x) {
this . x= x;
}
public void setY ( Object y) {
this . y= y;
}
public Object getX ( ) {
return this . x;
}
public Object getY ( ) {
return this . y;
}
}
public class 泛型_问题引出
{
public static void main ( String agrs[ ] ) {
Point point= new Point ( ) ;
point. setX ( 20 ) ;
point. setY ( "东经120度" ) ;
int x= ( Integer) point. getX ( ) ;
int y= ( Integer) point. getY ( ) ;
System. out. println ( "X=" + x+ ",Y=" + y) ;
}
}
demo_泛型使用
class Point < T>
{
private T saidOne;
public void setOne ( T saidOne) {
this . saidOne= saidOne;
}
public void said ( ) {
System. out. println ( "One=" + this . saidOne) ;
}
}
public class 泛型_使用
{
public static void main ( String agrs[ ] ) {
Point< Integer> point= new Point < Integer> ( ) ;
point. setOne ( 1 ) ;
fun ( point) ;
funA ( point) ;
Point< String> pointB= new Point ( ) ;
pointB. setOne ( "Hello" ) ;
funB ( pointB) ;
}
public static void fun ( Point< ? > temp) {
temp. said ( ) ;
}
public static void funA ( Point< ? extends Number > temp) {
temp. said ( ) ;
}
public static void funB ( Point< ? super String> temp) {
temp. said ( ) ;
}
}
demo_泛型接口与方法
interface IMessage < T>
{
public abstract String echo ( T mess) ;
}
class MessageA < S> implements IMessage < S>
{
public String echo ( S mess) {
return "消息:" + mess;
}
}
class MessageB implements IMessage < String>
{
public String echo ( String mess) {
return "消息:" + mess;
}
}
public class 泛型_接口和方法
{
public static void main ( String agrs[ ] ) {
IMessage< String> mess= new MessageA < String> ( ) ;
System. out. println ( mess. echo ( "Hello" ) ) ;
IMessage< String> mes= new MessageB ( ) ;
System. out. println ( mes. echo ( "Hello" ) ) ;
fun ( 3.14 ) ;
}
public static < T> void fun ( T temp) {
System. out. println ( temp) ;
}
}