多态图形的java代码 多态 java代码

下面是一个关于java多态的代码,我没看懂是什么意思,希望各位大神帮忙解答一下?

你好,很高兴回答你的问题。

在林芝等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、网站制作 网站设计制作按需制作网站,公司网站建设,企业网站建设,品牌网站建设,全网整合营销推广,成都外贸网站制作,林芝网站建设费用合理。

请看图中红线标识的位置,int变量c是调用方法的对象b的一个属性,在第一次执行b.a(new C())时,b对象的c变量已经执行c++变成了1了,在执行b.a(new D())时输出变量c时,自然就是1了。

如果有帮助到你,请点击采纳。

编写Java Applet程序画圆 矩形和直线,继承实现(多态实现)各个类都能实现构造方法

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]; //记录鼠标拖动的起始点和终点

public PainterPanel(){

super(); //调用父类构造函数

this.setBackground(Color.white); //设置背景颜色

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

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

addMouseListener(this); //增加鼠标事件

}

public void mouseReleased(MouseEvent e){ //鼠标释放事件

point[1]=new Point(e.getX(),e.getY()); //设置终点位置

repaint(); //重绘屏幕

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void mousePressed(MouseEvent e){ //鼠标按下时事件

point[0]=new Point(e.getX(),e.getY()); //设置起始点位置

}

public void paint(Graphics g){

super.paint(g);

switch (shape){ //根据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]; //记录鼠标拖动的起始点和终点

public PainterPanel ()

{

super ();

this.setBackground(Color.white);//设置背景颜色

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

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

addMouseListener (this);//增加鼠标事件

}

public void mouseReleased(MouseEvent e)//鼠标释放事件

{

point[1]=new Point(e.getX(),e.getY()); //设置终点位置

repaint(); //重绘屏幕

}

public void mouseEntered (MouseEvent e){}

public void mouseExited (MouseEvent e){}

public void mouseClicked (MouseEvent e){}

public void mousePressed (MouseEvent e) //鼠标按下时事件

{

point[0] = new Point(e.getX(), e.getY()); //设置起始点位置

}

public void paint(Graphics g)

{

super.paint(g);

switch (shape) //根据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]; //按钮组 创建最初未选定的切换按钮,不设置文本或图像。

PainterPanel painter = new PainterPanel(); //绘图面板

public PainterDemo()

{

super("Java画图程序"); //调用父类构造函数

String[] buttonName = {"直线", "椭圆", "矩形"}; //按钮文字

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

JToolBar toolBar = new JToolBar(); //实例化工具栏

ButtonGroup buttonGroup = new ButtonGroup(); //实例化按钮组

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

{

button[i] = new JButton (buttonName[i]); //实例化按钮

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); //设置窗口尺寸

setVisible(true); //设置窗口为可视

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序

}

class DrawShapeListener implements ActionListener //按钮事件处理

{

public void actionPerformed(ActionEvent e)

{

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

{

if (e.getSource() == button[i]) //判断来自于哪个按钮

{

painter.drawShape(i); //绘制图形

}

}

}

}

public static void main(String[] args)

{

new PainterDemo();

}

}

用java编程。利用多态编程创建一个Square类,实现求三角形,正方形和圆形的面积

首先要声明一下LZ问题描述的有点问题,

Square的意思是正方形,

所以应该是正方形(Square),三角形(Triangle),圆(Circle)来继承图形(Shape)来实现多态,即利用多态编程创建一个Shape类.

补充:1楼,不知道你的多态体现在哪里????

进入正题...

在同一个包下分别建立以下五个类,运行TestShape类即可!

// 抽象类--图形--------------------------------------

public abstract class Shape {

//抽象方法取得图形的面积

public abstract double getArea();

}

// 正方形继承图形的类-------------------------------

public class Square extends Shape {

// 正方形的边长

private double height = 0;

// 构造函数

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;

// 构造函数

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;

// 构造函数,已知三角形的高和底

public Triangle(double a, double h) {

this.a = a;

this.h = h;

}

// 构造函数,已知三角形的三边长度

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

this.a = a;

this.b = b;

this.c = c;

}

@Override

public double getArea() {

if (h == 0) {

// 根据海伦公式求三角形的面积

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

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

} else {

// 普通公式

return ( a * h / 2);

}

}

}

//测试类--------------------------------------------

public class TestShape {

public static void main(String[] args) {

// 构造一个边长为3的正方形

Shape square = new Square(3) ;

// 构造一个半径为2的圆

Shape circle = new Circle(2) ;

// 构造一个边长分别为3,4,5的三角形(根据勾股定理知道是直角三角形)

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

// 构造一个高和底分别为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());

}

}

测试结果:

9.0

12.56

6.0

6.0

好的答案不仅能够帮助LZ解决问题,还能够给阅读者带来收益!

当前题目:多态图形的java代码 多态 java代码
标题网址:https://www.cdcxhl.com/article28/ddiehcp.html

成都网站建设公司_创新互联,为您提供域名注册静态网站微信公众号企业网站制作定制网站网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司