Java 中的构造函数方法 I 构造函数重载
来源:Objectientedprogramming77 什么是构造函数?这是一种特殊类型的方法,其名称与类的名称相同,它决定了对象创建时如何初始化。 和其他方法一样,我们也可以在Java程序中定义一个Constructor方法,但是与其他方法不同的是,我们不能直接调用Constructor;Java 在创建对象时自动调用构造函数。当我们使用new关键字创建类的对象时,Java做了三件事:- 为对象分配内存。
- 使用初始值或默认值初始化此对象实例变量。
- 调用类的 Method 构造函数。
基本构造函数
根据定义,构造函数看起来像常规方法,但有两个主要区别。- 构造函数和类名始终相同。
- 它没有返回类型
class Person
{
String name;
int age;
Person (String n, int a)
{
name = n;
age = a;
}
void printPerson ()
{
System.out.print("Hi, I am " +name);
System.out.println(" I am "+ age + " years old.");
}
public static void main(String args[])
{
Person p;
p = new Person ("Ajab", 20);
p.printPerson();
p = new Person ("Rizwan", 30);
p.printPerson();
我们得到输出:
嗨,我是阿贾布。我20岁了。嗨,我是里兹万。我今年30岁
构造函数重载
与其他方法一样,构造函数也可以接受不同数量和类型的参数,允许创建具有指定属性的对象,或者允许构造函数根据不同类型的输入计算属性。例如,给定表中的MyRectone类创建一个MyRectone构造函数并传递不同的参数,而不是为给定参数创建不同的方法。class MyRectone
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
MyRectone ( int x1, int x2, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
MyRectone (Point topLeft, Point bottomRight)
{
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
}
MyRectone ( Point topLeft, int w, int h)
{
x1 = topLeft.x;
y1 = top left.y;
x2 = ( x1 + w);
y2 = (y1 + h);
}
void printRect ()
{
System.out.print ("MyRectone: <" + x1 + ", " + y1);
system.out.println (", " + x2 + " ,"+ y2 + ">");
}
public static void main (String args [] )
{
MyRectone rect;
System.out.println ("Calling MyRectone with coordinates 35,35 70,70");
rect = new MyRectone (35,35,70,70);
rect.printRect();
System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)");
rect = new MyRectone (15,15,30,30);
rect.printRect();
System.out.print (" Calling buildRect w/1 point (10,10),");
System.out.println ("width (50) and height (50)");
rect = new MyRectone ( new Point (10,10), 50, 50);
rect.printRect();
结论:
使用坐标 35,35 70,70 调用 MyRectone:MyRectone:<35,35,70,70> 调用 w/1 点 (15,15)、(30,30) 的 buildRect:MyRectone:<15,15,30,30 > 调用 buildRect w/1 点 (10,10)、宽度 (50) 和高度 (50):MyRectone:<10,10,50,50>
调用另一个构造函数
构造函数可以是类中定义的另一个构造函数的超集;也就是说,它们的行为可以相同,但还要多一些。与其在类中的多个构造函数方法中重复相同的行为,不如能够简单地从第二个构造函数的主体中调用第一个构造函数。Java 为此提供了一种特殊的语法。要调用当前类中定义的构造函数,请使用以下形式:this (arg1, arg2, arg3… ..);
当然, 这个 参数是构造函数的参数。
抽象 Java 类和接口
来源:Dev.to 抽象类是具有一些没有完整定义的方法并具有abstract修饰符的类。- 您不能使用抽象类的构造函数创建对象。
- 您可以使用抽象类作为基类来定义派生类。
- 抽象类至少有一个抽象方法。
- 抽象方法:与常规方法一样有一个标头,但没有主体,并且需要abstract修饰符和分号。
- 抽象方法不能是私有的。
- 抽象类可以是一种类型。
public abstract class Example{
private String data1;
private double data2;
public abstract double getPay();
}
接口:定义任何实现此接口的类都必须具有的一组方法。
- 接口是一种类型。
- 它包含没有定义和实例变量的方法头:
public interface Interface1{
public void method1();
public int method2();
}
要实现接口,类必须做两件事:
- 包括实现 InterfaceName。
- 该类必须实现接口中列出的所有方法头。
public class Implementer implements Interface1 {
@Override
public void method1() {
//definition
}
@Override
public int method2() {
//definition
}
}
- 方法头声明为public。
- 抽象类还可以实现接口,该类为接口中的一些方法头提供定义。
- Java接口还可以包含常量,例如:
public interface Constant {
public static final int JANUARY = 1, FEBRUARY = 2, MARCH = 3;
}
- 任何实现Constant接口的类都会自动拥有这些常量,例如:
public class Constants implements Constant {
public static void main(String[] args) {
System.out.println(JANUARY);
}
}
- 您可以通过将常量和方法头包含到单个接口中来混合使用接口。
- Java不支持多重继承,因此一个类只能扩展一个基类。但是,使用接口,一个类可以实现多个接口:
public class Implementer implements Interface1, Interface2, .. InterfaceN{
}
- 之所以一个Java类只能扩展一个基类,是因为如果Java允许有两个基类,那么两个类可能有相同的方法头,但定义不同,导致不一致。
- 如果定义两个具有相同名称和不同值的常量,两个接口可能不兼容:
public interface Interface1{
public static final int ANSWEAR = 0;
}
public interface Interface1{
public static final int ANSWEAR = 3;
}
GO TO FULL VERSION