本例以Box类为例,比较两种语言基本的类实例区别
Java:
package practice; public class Box { public int length; private int height; protected int width; public Box(int length, int width, int height){ this.length = length; this.width = width; this.height = height; } public void putItem(){ System.out.println("盒子的长:"+this.length); System.out.println("盒子的宽:"+this.width); System.out.println("盒子的高:"+this.height); } public static void main(String[] args) { Box box = new Box(2,2,3); box.putItem(); } }
Ruby:
class Box def initialize(length,width,height) @box_length = length @box_width = width @box_height = height end def putItem puts "盒子的长:#@box_length" puts "盒子的宽:#@box_width" puts "盒子的高:#@box_height" end box1 = Box.new(5, 8, 9) box1.putItem end