开发者社区> jclian91> 正文

Java,Python,Scala比较(二)类与继承

简介: UML图如下: 完整的Java代码如下: SimpleGeometricObject.java public class SimpleGeometricObject { private String col...
+关注继续查看

UML图如下:
这里写图片描述
完整的Java代码如下:
SimpleGeometricObject.java

public class SimpleGeometricObject {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    /**Construct a default geometric object */
    public SimpleGeometricObject(){
        dateCreated = new java.util.Date();
    }

    /**Construct a geometric object with the specified color
     * and filled value*/
    public SimpleGeometricObject(String color, boolean filled) {
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }

    /**Return color*/
    public String getColor() {
        return color;
    }

    /**set a new color*/
    public void setColor(String color) {
        this.color = color;
    }

    /**Return filled. Since filled is boolean,
     * its better method is named isFilled*/
    public boolean isFilled() {
        return filled;
    }

    /**Set a new filled*/
    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    /**Get dateCreated*/
    public java.util.Date getDateCreated(){
        return dateCreated;
    }

    /**Return a string representation of this object*/
    public String toString() {
        return "created on "+dateCreated+"\ncolor: "+color+" and filled: "+filled;
    }
}

CircleFromSimpleGeometricObject.java

public class CircleFromSimpleGeometricObject extends SimpleGeometricObject{
    private double radius;

    public CircleFromSimpleGeometricObject() {}

    public CircleFromSimpleGeometricObject(double radius) {
        this.radius = radius;
    }

    public CircleFromSimpleGeometricObject(double radius, String color, boolean filled) {
        super(color, filled);
        this.radius = radius;   
    }

    /**Return radius*/
    public double getRadius() {
        return radius;
    }

    /**Set a new radius*/
    public void setRadius(double radius) {
        this.radius = radius;
    }

    /**Return area*/
    public double getArea() {
        return radius*radius*Math.PI;
    }

    /**Return diameter*/
    public double getDiameter() {
        return 2*radius;
    }

    /**Return perimeter*/
    public double getPerimeter() {
        return 2*radius*Math.PI;
    }

    /**Return the circle info*/
    public void printCircle() {
        System.out.println("The circle is created "+getDateCreated()+" and the radius is "+radius);
    }   
}

RectangleFromSimpleGeometricObject.java

public class RectangleFromSimpleGeometricObject extends SimpleGeometricObject{
    private double width;
    private double height;

    public RectangleFromSimpleGeometricObject() {}

    public RectangleFromSimpleGeometricObject(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public RectangleFromSimpleGeometricObject(double width, double height, String color, boolean filled) {
        super(color, filled);
        this.width = width;
        this.height = height;
    }

    /**Return width*/
    public double getWidth() {
        return width;
    }

    /**Set a new width*/
    public void setWidth(double width) {
        this.width = width;
    }

    /**Return height*/
    public double getHeight() {
        return height;
    }

    /**Set a new width*/
    public void setHeight(double height) {
        this.height=height;
    }   

    /**Return area*/
    public double gerArea() {
        return width*height;
    }

    /**Return perimeter*/
    public double getPerimeter() {
        return 2*(width+height);
    }
}

Test.java

public class Test{
    public static void main(String[] args){
        CircleFromSimpleGeometricObject circle = new CircleFromSimpleGeometricObject(1,"red", true);
        System.out.println("A circle "+circle.toString());
        System.out.println("The color is "+circle.getColor());
        System.out.println("The radius is "+circle.getRadius());
        System.out.println("The area is "+circle.getArea());
        System.out.println("The diameter is "+circle.getDiameter());

        RectangleFromSimpleGeometricObject rect = new RectangleFromSimpleGeometricObject(2,4);
        System.out.println("\nA rectangle is "+rect.toString());
        System.out.println("The area is "+rect.gerArea());
        System.out.println("The perimeter is "+rect.getPerimeter());
    }
}

完整的Python代码如下:

import math
import datetime

class SimpleGeometricObject(object):
    def __init__(self, color, filled):
        self.__color = color
        self.__filled = filled 
        self.__date = datetime.datetime.now()

    def getColor(self):
        return self.__color

    def setColor(self, color):
        self.__color = color

    def isFilled(self):
        return self.__filled

    def setFilled(self, filled):
        self.__filled = filled

    def getDateCreated(self):
        return self.__date.strftime("%Y/%m/%d/%H:%M")

    def __str__(self):
        return  "is created on "+self.getDateCreated()+", color: "+self.__color\
                +" and filled: "+str(self.__filled)

class CircleFromSimpleGeometricObject(SimpleGeometricObject):
    def __init__(self, color, filled, radius):
        super(CircleFromSimpleGeometricObject, self).__init__(color, filled)
        self.__radius = radius

    def getRadius(self):
        return self.__radius

    def setRadius(self, radius):
        self.__radius = radius

    def getArea(self):
        return self.__radius*self.__radius*math.pi

    def getPerimeter(self):
        return 2*self.__radius*math.pi

    def getDiameter(self):
        return self.__radius*2

    def printCircle(self):
        print(self.__str__()+" and the radius is "+str(self.__radius))

class RectangleFromSimpleGeometricObject(SimpleGeometricObject):
    def __init__(self, color, filled, width, height):
        super(RectangleFromSimpleGeometricObject, self).__init__(color, filled)
        self.__width = width
        self.__height = height

    def getWidth(self):
        return self.__width

    def setWidth(self, width):
        self.__width = width

    def getHeight(self):
        return self.__height

    def setHeight(self, height):
        self.__height = height 

    def getArea(self):
        return self.__width * self.__height

    def getPerimeter(self):
        return 2*(self.__width+self.__height)

def main():
    circle = CircleFromSimpleGeometricObject("red", True, 5)
    print("A circle ", circle)
    print("The color is ", circle.getColor())
    print("The radius is %s"%circle.getRadius())
    print("The area is %s"%circle.getArea())
    print("The diameter is %s"%circle.getDiameter())

    rect = RectangleFromSimpleGeometricObject("white", False, 3, 4);
    print("\nA rectangle", rect);
    print("The area is %s"%rect.getArea());
    print("The perimeter is %s"%rect.getPerimeter());

main()

完整的Scala代码如下:

import java.util.Date;
import scala.math._;

object demo extends App {
  class SimpleGeometricObject (private var color:String="white", private var filled:Boolean=false){
    private var dateCreated:java.util.Date = new Date()

    def getColor() = color

    def setColor(color:String) = {this.color = color}

    def isFilled() = filled

    def setFilled(filled:Boolean) = {this.filled = filled}

    def getDateCreated() = dateCreated 

    override def toString()= "created on "+dateCreated+"\ncolor: "+color+" and filled: "+filled
  }

  class CircleFromSimpleGeometricObject(color:String, filled:Boolean, private var radius:Double) extends SimpleGeometricObject(color,filled){
    def this() = this("white", false, 1.0)

    def getRadius() = radius

    def setRadius(radius:Double) = {this.radius = radius}

    def getArea() = radius*radius*Pi

    def getDiameter() = radius*2

    def getPerimeter() = radius*2*Pi

    def printCircle() = {println("The circle is created "+getDateCreated+" and the radius is "+radius)}
  }

  class RectangleFromSimpleGeometricObject(color:String, filled:Boolean, private var width:Double, private var height:Double) extends SimpleGeometricObject(color, filled) {
    def this() = this("white", false, 1.0, 1.0)

    def getWidth() = width

    def setWidth(width:Double) = {this.width = width}

    def getHeight() = height

    def setHeight(height:Double) = {this.height = height}

    def getArea() = width*height

    def getPerimeter() = 2*(width+height)
  }

  var circle = new CircleFromSimpleGeometricObject("red", true, 2.0)
  println("A circle is "+circle.toString())
  println("The color is "+circle.getColor())
  println("The radius is "+circle.getRadius())
  println("The area is "+circle.getArea())
  println("The diameter is "+circle.getPerimeter()) 

  var rect = new RectangleFromSimpleGeometricObject("green", true, 3.0, 4.0)
  println("\nA rectangle is "+rect.toString())
  println("The area is "+rect.getArea())
  println("The perimeter is "+rect.getPerimeter())
}

读者可以自己从上述代码中体验三种语言的相同点与不同之处,显然,Python与Scala语言较为简洁~~



本次分享到此结束,欢迎大家交流~~

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
scala面向对象编程之继承
scala使用extends关键字来实现继承。可以在子类中定义父类中没有的字段和方法,或者重写父类的方法。 示例1:实现简单继承
11 0
Python:jpype模块调用Java函数
Python:jpype模块调用Java函数
21 0
Scala 的继承注意事项|学习笔记
快速学习 Scala 的继承注意事项。
20 0
Scala 的继承快速入门|学习笔记
快速学习 Scala 的继承快速入门。
18 0
历届真题 小朋友崇拜圈【第九届】【省赛】【C组】——【C++】【C】【Java】【Python】四种语言解法
历届真题 小朋友崇拜圈【第九届】【省赛】【C组】——【C++】【C】【Java】【Python】四种语言解法
37 0
蓝桥杯官网 试题 PREV-106 历届真题 修改数组【第十届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
蓝桥杯官网 试题 PREV-106 历届真题 修改数组【第十届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
46 0
蓝桥杯官网 试题 PREV-278 历届真题 双向排序【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
蓝桥杯官网 试题 PREV-278 历届真题 双向排序【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
61 0
蓝桥杯官网 试题 PREV-281 历届真题 时间显示【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
蓝桥杯官网 试题 PREV-281 历届真题 时间显示【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
52 0
蓝桥杯官网 试题 PREV-284 历届真题 杨辉三角形【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
蓝桥杯官网 试题 PREV-284 历届真题 杨辉三角形【第十二届】【省赛】【研究生组】【C++】【C】【Java】【Python】四种解法
192 0
浅谈C、Java与Python之间的小差异
C、Java、Python之间的小差异。变量是容器还是标签?Python的for循环为什么不一样?Python为什么不能用i++自增?C语言为什么比较快?Java的JVM是干什么的?C、Java、Python有什么区别,又有什么相似点。..................
35 0
+关注
jclian91
热爱算法,热爱技术,热爱生活,期待更好的自己与明天~
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Just Enough Scala for Spark
立即下载
JDK8新特性与生产-for“华东地区scala爱好者聚会”
立即下载
JDK8新特性与生产-for“华东地区scala爱好者聚会”
立即下载