public class Cons {
public Cons() {// A no arguments constructor that sends default values to the largestthis(madeUpArg1Value,madeUpArg2Value,madeUpArg3Value);}
public Cons(int arg1, int arg2) {// An example of a partial constructor that uses the passed in arguments// and sends a hidden default value to the largestthis(arg1,arg2, madeUpArg3Value);}
// Largest constructor that does the workpublic Cons(int arg1, int arg2, int arg3) {this.arg1 = arg1;this.arg2 = arg2;this.arg3 = arg3;}}
您还可以使用最近提倡的value eOf方法或仅使用“of”:
public class Cons {public static Cons newCons(int arg1,...) {// This function is commonly called valueOf, like Integer.valueOf(..)// More recently called "of", like EnumSet.of(..)Cons c = new Cons(...);c.setArg1(....);return c;}}
public class Rectangle {private int x, y;private int width, height;
public Rectangle() {this(1, 1);}public Rectangle(int width, int height) {this( 0,0,width, height);}public Rectangle(int x, int y, int width, int height) {this.x = x;this.y = y;this.width = width;this.height = height;}
}
class This1{This1(){this("Hello");System.out.println("Default constructor..");}This1(int a){this();System.out.println("int as arg constructor..");}This1(String s){System.out.println("string as arg constructor..");}
public static void main(String args[]){new This1(100);}}
class Example{private int a = 1;Example(){this(5); //here another constructor called based on constructor argumentSystem.out.println("number a is "+a);}Example(int b){System.out.println("number b is "+b);}
Class Test {Test() {this(10); // calls the constructor with integer args, Test(int a)}Test(int a) {this(10.5); // call the constructor with double arg, Test(double a)}Test(double a) {System.out.println("I am a double arg constructor");}}
class ConstructorDemo{ConstructorDemo()//Default Constructor{System.out.println("D.constructor ");}
ConstructorDemo(int k)//Parameterized constructor{this();//-------------(1)System.out.println("P.Constructor ="+k);}
public static void main(String[] args){//this(); error because "must be first statement in constructornew ConstructorDemo();//-------(2)ConstructorDemo g=new ConstructorDemo(3);---(3)}}
public class Product {private int productId;private String productName;private double productPrice;private String category;
public Product(int id, String name) {this(id,name,1.0);}
public Product(int id, String name, double price) {this(id,name,price,"DEFAULT");}
public Product(int id,String name,double price, String category){this.productId=id;this.productName=name;this.productPrice=price;this.category=category;}}
所以,下面这样的东西是行不通的。
public Product(int id, String name, double price) {System.out.println("Calling constructor with price");this(id,name,price,"DEFAULT");}
此外,在继承的情况下,当创建子类的对象时,首先调用超类构造函数。
public class SuperClass {public SuperClass() {System.out.println("Inside super class constructor");}}public class SubClass extends SuperClass {public SubClass () {//Even if we do not add, Java adds the call to super class's constructor like// super();System.out.println("Inside sub class constructor");}}
class MyConstructorDemo extends ConstructorDemo{MyConstructorDemo(){this("calling another constructor");}MyConstructorDemo(String arg){System.out.print("This is passed String by another constructor :"+arg);}}
public class SomeClass{
private int number;private String someString;
public SomeClass(){number = 0;someString = new String();}
public SomeClass(int number){this(); //set the class to 0this.setNumber(number);}
public SomeClass(int number, String someString){this(number); //call public SomeClass( int number )this.setString(someString);}
public void setNumber(int number){this.number = number;}public void setString(String someString){this.someString = someString;}//.... add some accessors}
这里有一些额外的小信贷:
public SomeOtherClass extends SomeClass {public SomeOtherClass(int number, String someString){super(number, someString); //calls public SomeClass(int number, String someString)}//.... Some other code.}
import java.util.*;import java.lang.*;
class Test{public static void main(String args[]){Dog d = new Dog(); // Both Calling Same Constructor of Parent Class i.e. 0 args Constructor.Dog cs = new Dog("Bite"); // Both Calling Same Constructor of Parent Class i.e. 0 args Constructor.
// You need to Explicitly tell the java compiler to use Argument constructor so you need to use "super" key wordSystem.out.println("------------------------------");Cat c = new Cat();Cat caty = new Cat("10");
System.out.println("------------------------------");// Self s = new Self();Self ss = new Self("self");}}
class Animal{String i;
public Animal(){i = "10";System.out.println("Animal Constructor :" +i);}public Animal(String h){i = "20";System.out.println("Animal Constructor Habit :"+ i);}}
class Dog extends Animal{public Dog(){System.out.println("Dog Constructor");}public Dog(String h){System.out.println("Dog Constructor with habit");}}
class Cat extends Animal{public Cat(){System.out.println("Cat Constructor");}public Cat(String i){super(i); // Calling Super Class Paremetrize Constructor.System.out.println("Cat Constructor with habit");}}
class Self{public Self(){System.out.println("Self Constructor");}public Self(String h){this(); // Explicitly calling 0 args constructor.System.out.println("Slef Constructor with value");}}
class User {private long id;private String username;private int imageRes;
public User() {init(defaultID,defaultUsername,defaultRes);}public User(String username) {init(defaultID,username, defaultRes());}
public User(String username, int imageRes) {init(defaultID,username, imageRes);}
public User(long id, String username, int imageRes) {init(id,username, imageRes);
}
private void init(long id, String username, int imageRes) {this.id=id;this.username = username;this.imageRes = imageRes;}}
public class Animal {private int animalType;
public Animal() {this(1); //here this(1) internally make call to Animal(1);}
public Animal(int animalType) {this.animalType = animalType;}}