var Square = require("./square"); // you can use `new` keyword var s = new Square(5); s.area(); // 25 // or you can skip it! var s2 = Square(10); s2.area(); // 100
function defclass(prototype) { var constructor = prototype.constructor; constructor.prototype = prototype; return constructor; } var Square = defclass({ constructor: function (side) { this.side = side; }, area: function () { return this.side * this.side; } }); var square = new Square(10); alert(square.area()); // 100
function Square(){ this.className = "Square"; this.corners = 4; } var MySquare = new Square(); console.log(MySquare.className); // "Square"
//Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square) rs.add(new Square());
Shape s[] = new Shape[2]; s[0] = new Circle(10); s[1] = new Square(10); System.out.println("Area of s[0] "+s[0].area()); System.out.println("Area of s[1] "+s[1].area());
//Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure) Square ss = new Square(); ss.add(rs);
public class Scene2 extends Scene { Square square1, square2; public Scene2(Context context) { super (context); // TODO Auto-generated constructor stub } public void init (GL10 gl) { square1 = new Square (); square2 = new Square (); } public void load (GL10 gl) { image (gl, R.drawable.s1_clouds); image (gl, R.drawable.s1_ground); } public void draw (GL10 gl) { square1.draw (gl, images.get(R.drawable.s1_clouds), 0, 0, width, height, 0); square1.draw (gl, images.get(R.drawable.s1_ground), 0, 0, width, height, 0); } }
var sq = new Square(5); alert(sq.area()); // 25 alert(sq instanceof Square); // true alert(sq instanceof Rectangle); // true alert(sq.constructor === Square); // true
Square[][] curr = new Square[4][]; for (int ctrOut = 0; ctrOut < 4; ctrOut++) { curr[ctrOut] = new Square[5]; for (int ctrIn = 0; ctrIn < 5; ctrIn++) { curr[ctrOut][ctrIn] = new Square(); } }
function Square(length){ this.sideLength = length; } Square.prototype.calcPerimeter = function() { return this.sideLength * 4; }; Square.prototype.calcArea = function() { return this.sideLength * this.sideLength ; }; var square = new Square(5); console.log(square.calcPerimeter()); console.log(square.calcArea());
Rectangle myBox; if (length == width) { myBox = new Square(); } else { myBox = new Rectangle(); }
String ans = JOptionPane.showInputDialog(null, "S,T,R,K or P?"); if (ans.equals("S")) { Square s = new Square(); }
>java -jar org.eclipse.jdt.core_3.10.2.v20150120-1634.jar -source 1.7 GenericTest.java ---------- 1. ERROR in C:\projects\Test\src\GenericTest.java (at line 32) Square maxSquare = findMax(new Square[]{new Square(1), new Square(2), new Square(3)}); ^^^^^^^ Bound mismatch: The generic method findMax(S[]) of type GenericTest is not applicable for the arguments (GenericTest.Square[]). The inferred type GenericTest.Square is not a valid substitute for the bounded parameter <S extends Comparable<S>> ---------- 1 problem (1 error)
// Not clear why this is an instance variable - and please make fields private :) List<Square> test = new ArrayList<Square>(); public void main() { test.add(new Square(10)); Iterator<Square> i = test.iterator(); while(i.hasNext()) { Square temp = i.next(); // Use square here } }
List<Square> test = new ArrayList<Square>(); public void main(){ test.add(new Square(10)); Iterator<Square> i = test.iterator(); while(i.hasNext()){ Square temp = i.next(); } }
public Square newInstance( int n ) { Square newFragment = new Square(); Bundle args = new Bundle(); //create a constant instead of the string 'number' args.putInt("number", n); newFragment.setArguments(args); return newFragment; }
class Canvas{ public static void main(String[] args){ List<Shape> shapes = new ArrayList<>(); shapes.add(new Circle()); shapes.add(new Square()); // clean and neat code for(Shape shape : shapes){ shape.draw(); } } }
public static void main( String[] args ) { Shape shape = new Triangle(); shape.draw(); // prints Draw a Triangle shape = new Square(); shape.draw(); // prints Draw a Square shape = new Shape(); shape.draw(); // prints Draw a Shape }