使用构造函数分配和初始化超类成员的Java程序
Java中的构造函数是一种用于初始化对象的特殊方法。每当使用 new 关键字创建对象时,至少会调用一个构造。构造函数名称必须与类名称匹配,并且不能有返回类型。如果在这种情况下类中没有可用的构造函数,则Java编译器默认提供默认构造函数(无参数构造函数)。
参数化构造函数:参数化构造函数用于用我们自己的值初始化类的字段。
例子:
Java
// Parameterized Constructor Example in Java
import java.io.*;
class parameterizedConstructor {
// fields of the class
String name;
int regestrationNumber;
// creating a parameterized constructor so that we can
// initialize the value of the class
parameterizedConstructor(String name,
int regestrationNumber)
{
System.out.println("constructor call");
this.name = name;
this.regestrationNumber = regestrationNumber;
}
}
class GFG {
public static void main(String[] args)
{
// creating our first object
parameterizedConstructor obj1
= new parameterizedConstructor("Nilesh",
2021806);
System.out.println("Name of the student "
+ obj1.name);
System.out.println("Registration Number "
+ obj1.regestrationNumber);
// creating second object
parameterizedConstructor obj2
= new parameterizedConstructor("Bhaskar",
2021807);
System.out.println("Name of the student "
+ obj2.name);
System.out.println("Registration Number "
+ obj2.regestrationNumber);
}
}
Java
// Java Program to Allocate and Initialize Super Class
// Members Using Constructor
import java.util.*;
class y {
int b;
int c;
// parameterized constructor of class y
y(int b, int c)
{
this.b = b;
this.c = c;
System.out.println("Hi I am parent constructor");
System.out.println("multiplication of two number "
+ b + " and " + c + " is "
+ b * c);
}
}
class x extends y {
int a;
// parameterized constructor of class x
x(int b, int c, int a)
{
// calls constructor of y
super(b, c);
System.out.println(
"Hi I am child class constructor");
System.out.println("class field of x class is "
+ a);
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y
x obj = new x(3, 4, 5);
}
}
Java
// Java Program to Allocate and Initialize Super Class
// Members Using Constructor
import java.io.*;
class grandParent {
int grandParentAge;
String grandParentName;
// constructor
grandParent(int grandParentAge, String grandParentName)
{
this.grandParentAge = grandParentAge;
this.grandParentName = grandParentName;
}
}
class parent extends grandParent {
int parentAge;
String parentName;
// parameterized constructor
parent(int grandParentAge, String grandParentName,
int parentAge, String parentName)
{
// calls grandparent constructor
super(grandParentAge, grandParentName);
this.parentAge = parentAge;
this.parentName = parentName;
}
}
class child extends parent {
int childAge;
String childName;
// constructor
child(int grandParentAge, String grandParentName,
int parentAge, String parentName, int childAge,
String childName)
{
// calls parent constructor
super(grandParentAge, grandParentName, parentAge,
parentName);
this.childAge = childAge;
this.childName = childName;
}
public void dispalyDetails()
{
System.out.println("Name of grand parent "
+ grandParentName + " and he is "
+ grandParentAge + " year old");
System.out.println("Name of parent " + parentName
+ " and he is " + parentAge
+ " year old");
System.out.println("Name of child " + childName
+ " and he is " + childAge
+ " year old");
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class child
// this will invoke the constructor of child
// but before invoking the constructor of class
// child it will invoke the constructor of it's
// parent class which is parent but parent is child
// of grandparent class so, before invoking the
// constructor of parent class it will invoke the
// constructor of grandparent class
child obj = new child(85, "Pan Singh", 39,
"M.S.Dhoni", 5, "Ziva Dhoni");
obj.dispalyDetails();
}
}
输出
constructor call
Name of the student Nilesh
Registration Number 2021806
constructor call
Name of the student Bhaskar
Registration Number 2021807
在类层次结构中,根据层次结构调用构造函数,首先调用父类构造函数,然后调用子类构造函数
示例 1
Java
// Java Program to Allocate and Initialize Super Class
// Members Using Constructor
import java.util.*;
class y {
int b;
int c;
// parameterized constructor of class y
y(int b, int c)
{
this.b = b;
this.c = c;
System.out.println("Hi I am parent constructor");
System.out.println("multiplication of two number "
+ b + " and " + c + " is "
+ b * c);
}
}
class x extends y {
int a;
// parameterized constructor of class x
x(int b, int c, int a)
{
// calls constructor of y
super(b, c);
System.out.println(
"Hi I am child class constructor");
System.out.println("class field of x class is "
+ a);
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class x
// this will invoke the constructor of x
// but before invoking the constructor of class x
// it will invoke the constructor of it's parent
// class which is y
x obj = new x(3, 4, 5);
}
}
输出
Hi I am parent constructor
multiplication of two number 3 and 4 is 12
Hi I am child class constructor
class field of x class is 5
示例 2
Java
// Java Program to Allocate and Initialize Super Class
// Members Using Constructor
import java.io.*;
class grandParent {
int grandParentAge;
String grandParentName;
// constructor
grandParent(int grandParentAge, String grandParentName)
{
this.grandParentAge = grandParentAge;
this.grandParentName = grandParentName;
}
}
class parent extends grandParent {
int parentAge;
String parentName;
// parameterized constructor
parent(int grandParentAge, String grandParentName,
int parentAge, String parentName)
{
// calls grandparent constructor
super(grandParentAge, grandParentName);
this.parentAge = parentAge;
this.parentName = parentName;
}
}
class child extends parent {
int childAge;
String childName;
// constructor
child(int grandParentAge, String grandParentName,
int parentAge, String parentName, int childAge,
String childName)
{
// calls parent constructor
super(grandParentAge, grandParentName, parentAge,
parentName);
this.childAge = childAge;
this.childName = childName;
}
public void dispalyDetails()
{
System.out.println("Name of grand parent "
+ grandParentName + " and he is "
+ grandParentAge + " year old");
System.out.println("Name of parent " + parentName
+ " and he is " + parentAge
+ " year old");
System.out.println("Name of child " + childName
+ " and he is " + childAge
+ " year old");
}
}
class GFG {
public static void main(String[] args)
{
// creating an object of class child
// this will invoke the constructor of child
// but before invoking the constructor of class
// child it will invoke the constructor of it's
// parent class which is parent but parent is child
// of grandparent class so, before invoking the
// constructor of parent class it will invoke the
// constructor of grandparent class
child obj = new child(85, "Pan Singh", 39,
"M.S.Dhoni", 5, "Ziva Dhoni");
obj.dispalyDetails();
}
}
输出
Name of grand parent Pan Singh and he is 85 year old
Name of parent M.S.Dhoni and he is 39 year old
Name of child Ziva Dhoni and he is 5 year old