• JAVA学习日记32-0806


    今天学了什么

    用点类Point和圆类Circle设计圆锥类Cone

    Point类

    package cone;

    public class Point {

    protected double x,y;
    Point(double x,double y){
    this.x=x;
    this.y=y;
    }
    public void setx(double x) {
    this.x=x;
    }
    public void sety(double y) {
    this.y=y;
    }
    public double getx() {
    return x;
    }
    public double gety() {
    return y;
    }
    public void showPoint() {
    System.out.println("x="+x+",y="+y);
    }

    }

    Circle类

    package cone;

    public class Circle extends Point {
    protected final double PI=3.14159;
    protected double radius;
    public Circle(double x,double y,double radius) {
    super(x,y);
    this.radius=radius;
    }
    public void setr(double r) {
    this.radius=r;
    }
    public double getr() {
    return radius;
    }
    public double area() {
    return PI*radius*radius;
    }
    public double length() {
    return 2*PI*radius;
    }
    public void showCircle() {
    System.out.println("radius="+radius+",length="+length()+",area="+area());
    }
    }

    Cone类

    package cone;

    public class Cone extends Circle {
    private double h;
    public Cone(double x, double y, double radius, double h) {
    super(x, y, radius);
    this.h=h;
    }
    public void seth(double h) {
    this.h=h;
    }
    public double geth() {
    return h;
    }
    public double sarea() {
    double l = Math.sqrt (radius*radius + h*h);
    double s = PI*l*radius + PI*radius*radius;
    return s;
    }
    public double volume() {
    return (h*area())/3;
    }
    public void showCone() {
    System.out.println("h="+h+",sarea="+sarea()+",volume="+volume());
    }

    public static void main(String[] args) {
    Cone c = new Cone(0, 0, 0, 0);
    c.setx(1);
    c.sety(2);
    c.setr(3);
    c.seth(4);
    c.showPoint();
    c.showCircle();
    c.showCone();
    }

    }

    运行截图

    遇到的问题

    sqrt()方法要Math.sqrt()这样写

    明天计划

    练习面向对象习题

  • 相关阅读:
    vs编译出现 fatal error LNK1281:无法生成 SAFESEH 映像
    $apply()和$digest()——angular
    JS获取URL中参数值
    NeDB——node嵌入式数据库
    VS Code常用插件
    js断点调试
    VS Code 使用Git进行版本控制
    VS Code快捷键
    用户tokenId
    node-webkit-updater——NW.js自动更新
  • 原文地址:https://www.cnblogs.com/a8047/p/13446033.html
Copyright © 2020-2023  润新知