多態(tài)圖形的java代碼 多態(tài) java代碼

下面是一個(gè)關(guān)于java多態(tài)的代碼,我沒看懂是什么意思,希望各位大神幫忙解答一下?

你好,很高興回答你的問題。

在林芝等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站制作,林芝網(wǎng)站建設(shè)費(fèi)用合理。

請(qǐng)看圖中紅線標(biāo)識(shí)的位置,int變量c是調(diào)用方法的對(duì)象b的一個(gè)屬性,在第一次執(zhí)行b.a(new C())時(shí),b對(duì)象的c變量已經(jīng)執(zhí)行c++變成了1了,在執(zhí)行b.a(new D())時(shí)輸出變量c時(shí),自然就是1了。

如果有幫助到你,請(qǐng)點(diǎn)擊采納。

編寫Java Applet程序畫圓 矩形和直線,繼承實(shí)現(xiàn)(多態(tài)實(shí)現(xiàn))各個(gè)類都能實(shí)現(xiàn)構(gòu)造方法

PainterPanel.java 代碼:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class PainterPanel extends JPanel implements MouseListener{

int shape=-1; //圖案類型

Point[] point=new Point[2]; //記錄鼠標(biāo)拖動(dòng)的起始點(diǎn)和終點(diǎn)

public PainterPanel(){

super(); //調(diào)用父類構(gòu)造函數(shù)

this.setBackground(Color.white); //設(shè)置背景顏色

point[0]=new Point(-1,-1); //初始化變量

point[1]=new Point(-1,-1);

addMouseListener(this); //增加鼠標(biāo)事件

}

public void mouseReleased(MouseEvent e){ //鼠標(biāo)釋放事件

point[1]=new Point(e.getX(),e.getY()); //設(shè)置終點(diǎn)位置

repaint(); //重繪屏幕

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void mousePressed(MouseEvent e){ //鼠標(biāo)按下時(shí)事件

point[0]=new Point(e.getX(),e.getY()); //設(shè)置起始點(diǎn)位置

}

public void paint(Graphics g){

super.paint(g);

switch (shape){ //根據(jù)shape值繪制圖形

case 0:

g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //繪線

break;

case 1:

int width=point[1].x-point[0].x;

int height=point[1].y-point[0].y;

g.drawOval(point[0].x,point[0].y,width,height); //繪橢圓

break;

case 2:

width=point[1].x-point[0].x;

height=point[1].y-point[0].y;

g.drawRect(point[0].x,point[0].y,width,height); //繪矩形

break;

}

}

public void drawShape(int shape){

this.shape=shape;

}

}

PainterDemo.java 代碼

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class PainterPanel extends JPanel implements MouseListener

{

int shape = -1; //圖案類型

Point point[] = new Point[2]; //記錄鼠標(biāo)拖動(dòng)的起始點(diǎn)和終點(diǎn)

public PainterPanel ()

{

super ();

this.setBackground(Color.white);//設(shè)置背景顏色

point[0] = new Point (-1, -1);//初始化變量

point[1] = new Point (-1, -1);

addMouseListener (this);//增加鼠標(biāo)事件

}

public void mouseReleased(MouseEvent e)//鼠標(biāo)釋放事件

{

point[1]=new Point(e.getX(),e.getY()); //設(shè)置終點(diǎn)位置

repaint(); //重繪屏幕

}

public void mouseEntered (MouseEvent e){}

public void mouseExited (MouseEvent e){}

public void mouseClicked (MouseEvent e){}

public void mousePressed (MouseEvent e) //鼠標(biāo)按下時(shí)事件

{

point[0] = new Point(e.getX(), e.getY()); //設(shè)置起始點(diǎn)位置

}

public void paint(Graphics g)

{

super.paint(g);

switch (shape) //根據(jù)shape值繪制圖形

{

case 0:

g.drawLine (point[0].x, point[0].y, point[1].x, point[1].y); //繪線

break;

case 1:

int width = point[1].x - point[0].x;

int height= point[1].y - point[0].y;

g.drawOval (point[0].x, point[0].y, width, height); //繪橢圓

break;

case 2:

width = point[1].x - point[0].x;

height = point[1].y - point[0].y;

g.drawRect(point[0].x, point[0].y, width, height); //繪矩形

break;

}

}

public void drawShape(int shape)

{

this.shape=shape;

}

}

public class PainterDemo extends JFrame

{

JButton[] button = new JButton[3]; //按鈕組 創(chuàng)建最初未選定的切換按鈕,不設(shè)置文本或圖像。

PainterPanel painter = new PainterPanel(); //繪圖面板

public PainterDemo()

{

super("Java畫圖程序"); //調(diào)用父類構(gòu)造函數(shù)

String[] buttonName = {"直線", "橢圓", "矩形"}; //按鈕文字

DrawShapeListener buttonListener = new DrawShapeListener(); //按鈕事件

JToolBar toolBar = new JToolBar(); //實(shí)例化工具欄

ButtonGroup buttonGroup = new ButtonGroup(); //實(shí)例化按鈕組

for (int i = 0; i button.length; i ++)

{

button[i] = new JButton (buttonName[i]); //實(shí)例化按鈕

button[i].addActionListener (buttonListener); //增加按鈕事件處理

buttonGroup.add(button[i]); //增加按鈕到按鈕組

toolBar.add(button[i]); //增加按鈕到工具欄

}

Container container=getContentPane(); //得到窗口容器

container.add (toolBar,BorderLayout.NORTH); //增加組件到容器上

container.add (painter,BorderLayout.CENTER);

setSize(300,200); //設(shè)置窗口尺寸

setVisible(true); //設(shè)置窗口為可視

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序

}

class DrawShapeListener implements ActionListener //按鈕事件處理

{

public void actionPerformed(ActionEvent e)

{

for (int i = 0; i button.length; i ++)

{

if (e.getSource() == button[i]) //判斷來自于哪個(gè)按鈕

{

painter.drawShape(i); //繪制圖形

}

}

}

}

public static void main(String[] args)

{

new PainterDemo();

}

}

用java編程。利用多態(tài)編程創(chuàng)建一個(gè)Square類,實(shí)現(xiàn)求三角形,正方形和圓形的面積

首先要聲明一下LZ問題描述的有點(diǎn)問題,

Square的意思是正方形,

所以應(yīng)該是正方形(Square),三角形(Triangle),圓(Circle)來繼承圖形(Shape)來實(shí)現(xiàn)多態(tài),即利用多態(tài)編程創(chuàng)建一個(gè)Shape類.

補(bǔ)充:1樓,不知道你的多態(tài)體現(xiàn)在哪里????

進(jìn)入正題...

在同一個(gè)包下分別建立以下五個(gè)類,運(yùn)行TestShape類即可!

// 抽象類--圖形--------------------------------------

public abstract class Shape {

//抽象方法取得圖形的面積

public abstract double getArea();

}

// 正方形繼承圖形的類-------------------------------

public class Square extends Shape {

// 正方形的邊長

private double height = 0;

// 構(gòu)造函數(shù)

public Square(double height) {

this.height = height;

}

//@Override

public double getArea() {

return (this.height * this.height); // Math.pow(this.height , 2);

}

}

//圓繼承圖形的類-------------------------

public class Circle extends Shape {

// 圓的半徑

private double r = 0;

// 圓周率

private final static double PI = 3.14;

// 構(gòu)造函數(shù)

public Circle(double r) {

this.r = r;

}

@Override

public double getArea() {

return (PI * r * r);

}

}

//三角形繼承圖形的類------------------------------

public class Triangle extends Shape {

// 三角形的邊1

private double a = 0;

// 三角形的邊2

private double b = 0;

// 三角形的邊3

private double c = 0;

// 三角形的高

private double h = 0;

// 構(gòu)造函數(shù),已知三角形的高和底

public Triangle(double a, double h) {

this.a = a;

this.h = h;

}

// 構(gòu)造函數(shù),已知三角形的三邊長度

public Triangle(double a, double b, double c) {

this.a = a;

this.b = b;

this.c = c;

}

@Override

public double getArea() {

if (h == 0) {

// 根據(jù)海倫公式求三角形的面積

double s = (a+b+c)/2;

return Math.pow(s*(s-a)*(s-b)*(s-c), 0.5);

} else {

// 普通公式

return ( a * h / 2);

}

}

}

//測(cè)試類--------------------------------------------

public class TestShape {

public static void main(String[] args) {

// 構(gòu)造一個(gè)邊長為3的正方形

Shape square = new Square(3) ;

// 構(gòu)造一個(gè)半徑為2的圓

Shape circle = new Circle(2) ;

// 構(gòu)造一個(gè)邊長分別為3,4,5的三角形(根據(jù)勾股定理知道是直角三角形)

Shape triangle1 = new Triangle(3, 4, 5);

// 構(gòu)造一個(gè)高和底分別為3,4的三角形

Shape triangle2 = new Triangle(3, 4);

System.out.println(square.getArea());

System.out.println(circle.getArea());

System.out.println(triangle1.getArea());

System.out.println(triangle2.getArea());

}

}

測(cè)試結(jié)果:

9.0

12.56

6.0

6.0

好的答案不僅能夠幫助LZ解決問題,還能夠給閱讀者帶來收益!

當(dāng)前題目:多態(tài)圖形的java代碼 多態(tài) java代碼
標(biāo)題網(wǎng)址:http://muchs.cn/article28/ddiehcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、靜態(tài)網(wǎng)站微信公眾號(hào)、企業(yè)網(wǎng)站制作、定制網(wǎng)站、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司