public void drawSwitch(Graphics g,boolean closed,int dx){ g.fillOval(x-2,y-2,5,5) ; //a thick point if (closed) g.drawLine(x,y,x+dx,y);//the switch arm else g.drawLine(x,y,x+dx,y-20); x+=dx;//coordinates counting g.fillOval(x-2,y-2,5,5); //a thick point again }//end drawSwitch
public void drawBulb(Graphics g,int diameter){ if(statusOfSwitch1&&statusOfSwitch2){ g.setColor(Color.blue); g.fillOval(x,y-diameter/2,diameter,diameter);//lamp is on g.setColor(Color.blue); }//end if else {g.drawOval(x,y-diameter/2,diameter,diameter); g.setColor(Color.yellow);} //lamp is off x+=diameter;//coordinates counting }//end drawBulb
public void itemStateChanged(ItemEvent e){ statusOfSwitch1=box1.getState(); statusOfSwitch2=box2.getState(); repaint(); }//end itemStateChanged }//end class SerieKring
面向对象的 import java.awt.*; import java.applet.*; import java.awt.event.*; public class SerialLoop1 extends Applet { Circuit pcb; public void init() { pcb = new Circuit( this); } // end init
public void paint( Graphics g ) { pcb.draw(g); } // end paint
} // end class SerialLoop class Circuit { Switch s1, s2; Bulb b1; SwitchBox v1, v2; Wire d1, d2, d3; Circuit(Applet container) { s1 = new Switch( new Point(100,100), new Point(150,100)); d1 = new Wire( new Point(150,100), new Point(230,100)); s2 = new Switch( new Point(230,100), new Point(280,100)); d2 = new Wire( new Point(280,100), new Point(280,150)); b1 = new Bulb( new Point(100,150), new Point(280,150), 30); d3 = new Wire( new Point(100,100), new Point(100,150)); v1 = new SwitchBox( "Switch 1", container, this); v2 = new SwitchBox( "Switch 2", container, this); // SwitchBox.add( this); this.change(); // initial circuit logic state } // end Circuit void draw( Graphics g ) { s1.draw(g); d1.draw(g); s2.draw(g); d2.draw(g); b1.draw(g); d3.draw(g); } // end draw void change() { s1.closedIf(v1.getState()); s2.closedIf(v2.getState()); b1.onIf(s1.isClosed() && s2.isClosed()); } // end change } // end Circuit class Point { private int x, y; Point ( int x, int y) { this.x = x; this.y = y; } // end Point int x() { return x; } // end x int y() { return y; } // end y
void drawDot( Graphics g, int diameter, Color newColor) { Color nowColor = g.getColor(); g.setColor( newColor); g.fillOval( x-diameter/2, y-diameter/2, diameter, diameter); g.setColor( nowColor); } // end drawDot void drawLineTo( Graphics g, Point p) { g.drawLine( this.x(), this.y(), p.x(), p.y());} // end drawLineTo Point shift( int x, int y) { return new Point( this.x()+x, this.y()+y); } // end shift Point inBetween( Point p) { return new Point( (this.x()+p.x())/2, (this.y()+p.y())/2);} // end inBetween } // end class Point abstract class TwoPole { private Point in, out; // essential attributes TwoPole (Point in, Point out) { this.in = in; this.out = out;} // end TwoPole abstract void draw( Graphics g); Point in() { return in;} // end in Point out() { return out;} // end out } // end class TwoPoleclass Wire extends TwoPole { Wire (Point in, Point out) { super( in, out) ;} // end Wire void draw( Graphics g) { in().drawLineTo( g, out()); } // end draw } // end class Wire class Switch extends TwoPole { private boolean open; // open means no connection Switch(Point in, Point out) // constructor initialises position { super( in, out); } // end Switch void open() // set the switch open { open = true; } // end open void close() // close the switch { open = false; } // end close boolean isClosed() // answer true if the switch is closed { return !open; } // end isClosed void closedIf(boolean closed) // close the switch if closed = true { this.open = !closed; } // end closedIf void draw( Graphics g) { in().drawDot( g, 5, Color.black); // a dot of 5 pixels in diameter g.fillOval( in().x()- 2,out().x()- 2, 5, 5 ); if (!open ) in().drawLineTo( g, out() ); // switch arm is drawn closed else in().drawLineTo( g, out().shift( 0, -20)); // switch arm is drawn open out().drawDot( g, 5, Color.black); // a dot of 5 pixels in diameter } // end draw } // end class Switch
class Bulb extends TwoPole { private int diameter; private boolean on; Bulb ( Point in, Point out, int diameter) // initialises position and size { super( in, out); this.diameter = diameter; } // end Bulb void on() // put the lamp on { on = true; } // end on void off() // put the lamp off { on = false; } // end off boolean isOn() // true if lamp is on { return on; } // end isOpen void onIf( boolean on) // lamp is on if condition on is true { this.on = on; } // end onIf void draw( Graphics g) { in().drawLineTo( g, out()); // draw the wire Point center = in().inBetween( out()); // centre of drawing if (on) center.drawDot( g, diameter, Color.red); // red bulb, if on else center.drawDot( g, diameter, Color.black); // black bulb, if off } // end draw } // end class Bulb class SwitchBox extends Checkbox implements ItemListener { private Circuit commonCircuit; private Applet applet; SwitchBox(String identifier, Applet applet, Circuit commonCircuit) { super(identifier); this.addItemListener( this); applet.add(this); this.applet = applet; this.commonCircuit = commonCircuit; } // end SwitchBox
public void itemStateChanged( ItemEvent e ) { commonCircuit.change(); applet.repaint(); } // end itemStateChanged } // end class SwitchBox :
要做好OOP
细心体会身边所有的Object,
将道家思想应用于开发.
每个人都是从不会到会,从菜鸟到专家,我提个问题你不回答我也不用
嘲笑我吧,再说了,我问这个问题也不代表我的程序水平真的象你想的那么
差,没有面向对象概念时同样也出了很多计算机大师,面向对象只不过是
一种思考方法而已,没必要看的那么高深莫测。
而且各位大虾给我回答的时候请注意我的题目中有“核心”二字,这里
我用“核心”二字不是想让大虾们告诉我“面向对象就是封装继承多态”,
而是想让大虾们谈谈自己用面向对象思想指导自己编程的经验和方法以及
在具体代码方面和面向过程思想的差别。
我是做vb的,那里面不可能用完全的面向对象思想,而且c++为了向c
兼容也提供了一条可以以面向过程思想编程的道路,而且我没有具体用
过java,我无法完完全全的感受到面向对象思想,我没有办法完完全全
的脱离面向过程去写程序,请大虾们指点迷津。谢谢
接口:
public interface SaleProc(){
String regist();
void update()
}实现:
public class SaleProcImple() implements SaleProc{
String regist(){
return "";
}
void update(){ }
}
可以同过java来理解oop.
sun的blueprints是个很经典的oop,你可以看看.
过程--->函数 对象--->类
坏处: 数据和操作分离, 数据结构可以随便改动 那么:封装
函数---计算机模型-->不能真实的反映现实世界 现实世界
物质是有共性的:继承
物质是多样的:多态 封装是软件工程的需要-----模块化
继承和多态是认识(描述)现实世界(商业系统)的需要; 另外,封装是软件工程---模块化的需要
体现在: 封装不仅仅可以在类层次, 对于一个包(package)还有修饰;
(编译通过,applet);第一个是非面向对象的,看似简单,但是可移植性差,每向里面加一个元器件都得重写全部程序,第二个是面向对象的,看似复杂,但是分工明确,可移植性强,向里面添加任何元器件只需要加上这个元器件的
类即可了
非面向对象的
import java.awt.*;
import java.applet.*;
import java.awt.event.*;public class SeriaLoopnotoop extends Applet implements ItemListener{
private final int startX=100,startY=10;
private int x,y;
private Checkbox box1,box2;
private boolean statusOfSwitch1,statusOfSwitch2;
public void init(){
statusOfSwitch1=false;statusOfSwitch2=true ;
box1=new Checkbox("Switch1",statusOfSwitch1);
box1.addItemListener(this);
add(box1);
box2=new Checkbox("Switch2",statusOfSwitch2);
box2.addItemListener(this);
add(box2);
}
public void paint(Graphics g){
x=startX;y=startY;
drawSwitch(g,statusOfSwitch1,50);//(100,100)->(150,100)
drawWire(g,80,0);
drawSwitch(g,statusOfSwitch2,50);
drawWire(g,0,50);
x=startX;y=startY;
drawWire(g,0,50);
drawWire(g,75,0);
drawBulb(g,30);
drawWire(g,75,0);
}//end paint
public void drawSwitch(Graphics g,boolean closed,int dx){
g.fillOval(x-2,y-2,5,5) ; //a thick point
if (closed) g.drawLine(x,y,x+dx,y);//the switch arm
else g.drawLine(x,y,x+dx,y-20);
x+=dx;//coordinates counting
g.fillOval(x-2,y-2,5,5); //a thick point again
}//end drawSwitch
public void drawWire(Graphics g,int dx,int dy){
g.drawLine(x,y,x+dx,y);
x+=dx;
y+=dy;
}//end drawWire
public void drawBulb(Graphics g,int diameter){
if(statusOfSwitch1&&statusOfSwitch2){
g.setColor(Color.blue);
g.fillOval(x,y-diameter/2,diameter,diameter);//lamp is on
g.setColor(Color.blue);
}//end if
else {g.drawOval(x,y-diameter/2,diameter,diameter);
g.setColor(Color.yellow);}
//lamp is off
x+=diameter;//coordinates counting
}//end drawBulb
public void itemStateChanged(ItemEvent e){
statusOfSwitch1=box1.getState();
statusOfSwitch2=box2.getState();
repaint();
}//end itemStateChanged
}//end class SerieKring
面向对象的
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SerialLoop1 extends Applet
{ Circuit pcb; public void init()
{ pcb = new Circuit( this);
} // end init
public void paint( Graphics g )
{ pcb.draw(g);
} // end paint
} // end class SerialLoop class Circuit
{ Switch s1, s2;
Bulb b1;
SwitchBox v1, v2;
Wire d1, d2, d3; Circuit(Applet container)
{ s1 = new Switch( new Point(100,100), new Point(150,100));
d1 = new Wire( new Point(150,100), new Point(230,100));
s2 = new Switch( new Point(230,100), new Point(280,100));
d2 = new Wire( new Point(280,100), new Point(280,150));
b1 = new Bulb( new Point(100,150), new Point(280,150), 30);
d3 = new Wire( new Point(100,100), new Point(100,150));
v1 = new SwitchBox( "Switch 1", container, this);
v2 = new SwitchBox( "Switch 2", container, this);
// SwitchBox.add( this);
this.change(); // initial circuit logic state
} // end Circuit void draw( Graphics g )
{ s1.draw(g); d1.draw(g); s2.draw(g);
d2.draw(g); b1.draw(g); d3.draw(g);
} // end draw void change()
{ s1.closedIf(v1.getState());
s2.closedIf(v2.getState());
b1.onIf(s1.isClosed() && s2.isClosed());
} // end change } // end Circuit
class Point
{ private int x, y; Point ( int x, int y)
{ this.x = x; this.y = y; } // end Point int x()
{ return x; } // end x
int y()
{ return y; } // end y
void drawDot( Graphics g, int diameter, Color newColor)
{ Color nowColor = g.getColor();
g.setColor( newColor);
g.fillOval( x-diameter/2, y-diameter/2, diameter, diameter);
g.setColor( nowColor);
} // end drawDot void drawLineTo( Graphics g, Point p)
{ g.drawLine( this.x(), this.y(), p.x(), p.y());} // end drawLineTo Point shift( int x, int y)
{ return new Point( this.x()+x, this.y()+y); } // end shift Point inBetween( Point p)
{ return new Point( (this.x()+p.x())/2, (this.y()+p.y())/2);} // end inBetween } // end class Point
abstract class TwoPole
{ private Point in, out; // essential attributes TwoPole (Point in, Point out)
{ this.in = in; this.out = out;} // end TwoPole abstract void draw( Graphics g); Point in()
{ return in;} // end in
Point out()
{ return out;} // end out } // end class TwoPoleclass Wire extends TwoPole
{
Wire (Point in, Point out)
{ super( in, out) ;} // end Wire void draw( Graphics g)
{ in().drawLineTo( g, out()); } // end draw } // end class Wire
class Switch extends TwoPole
{ private boolean open; // open means no connection Switch(Point in, Point out) // constructor initialises position
{ super( in, out);
} // end Switch void open() // set the switch open
{ open = true;
} // end open void close() // close the switch
{ open = false;
} // end close boolean isClosed() // answer true if the switch is closed
{ return !open;
} // end isClosed void closedIf(boolean closed) // close the switch if closed = true
{ this.open = !closed;
} // end closedIf void draw( Graphics g)
{ in().drawDot( g, 5, Color.black); // a dot of 5 pixels in diameter
g.fillOval( in().x()- 2,out().x()- 2, 5, 5 );
if (!open ) in().drawLineTo( g, out() ); // switch arm is drawn closed
else in().drawLineTo( g, out().shift( 0, -20)); // switch arm is drawn open
out().drawDot( g, 5, Color.black); // a dot of 5 pixels in diameter
} // end draw } // end class Switch
class Bulb extends TwoPole
{ private int diameter;
private boolean on; Bulb ( Point in, Point out, int diameter) // initialises position and size
{ super( in, out);
this.diameter = diameter;
} // end Bulb void on() // put the lamp on
{ on = true;
} // end on void off() // put the lamp off
{ on = false;
} // end off boolean isOn() // true if lamp is on
{ return on;
} // end isOpen void onIf( boolean on) // lamp is on if condition on is true
{ this.on = on;
} // end onIf void draw( Graphics g)
{ in().drawLineTo( g, out()); // draw the wire
Point center = in().inBetween( out()); // centre of drawing
if (on) center.drawDot( g, diameter, Color.red); // red bulb, if on
else center.drawDot( g, diameter, Color.black); // black bulb, if off
} // end draw } // end class Bulb
class SwitchBox extends Checkbox implements ItemListener
{ private Circuit commonCircuit;
private Applet applet; SwitchBox(String identifier, Applet applet, Circuit commonCircuit)
{ super(identifier);
this.addItemListener( this);
applet.add(this);
this.applet = applet;
this.commonCircuit = commonCircuit;
} // end SwitchBox
public void itemStateChanged( ItemEvent e )
{ commonCircuit.change();
applet.repaint();
} // end itemStateChanged } // end class SwitchBox
: