使用构造函数为类创建对象并在对象中赋值的Java程序
Java是最流行的编程语言之一。它是一种面向对象的编程语言,这意味着我们可以创建类、对象等等。它还支持继承、多态、封装等等。它用于从移动应用程序到基于 Web 的应用程序的所有应用程序。类被定义为我们可以从中创建对象的蓝图或模板,对象是由状态和行为组成的类的实例。
方法:
- 首先,定义一个名为“SampleClass”的类并定义一个构造函数方法。
- 构造函数将始终具有与类名相同的名称,并且它没有返回类型。
- 构造函数用于实例化类的变量。
- 现在,我们可以使用构造函数来赋值。
- 在构造函数方法之后,实现一个返回任何变量值的函数。
- 现在在主函数中使用创建一个对象 “新”关键字。如果类没有用户定义的构造函数,则调用类的默认构造函数,否则在创建对象时,根据与类的构造函数匹配的参数类型和数量调用用户定义的构造函数。
- 我们可以使用对象调用上面声明的函数。
- 最后使用 System.out.println() 语句打印函数中的值。
- 完成实施后,运行程序并获得输出。
示例 1:
Java
// Java program to show the class declaration
// and how to create an instance of this class
// Class Declaration
public class Student
{
// Instance Variables
String name;
String course;
int age;
// Constructor Declaration of Class
public Student(String name, String course,int age)
{
this.name = name;
this.course = course;
this.age = age;
}
// method 1
public String getName()
{
return name;
}
public static void main(String[] args)
{
// creating object using new operator
Student s1 = new Student("Ravi","CSE",23);
System.out.println(s1.getName());
}
}
Java
// Java program to show the class declaration
// and how to create an instance of this class
// Class Declaration
public class Computer
{
// Instance Variables
String name;
String config;
int cost;
String os;
// Constructor Declaration of Class
public Computer(String name, String config,
int cost, String os)
{
this.name = name;
this.config = config;
this.cost = cost;
this.os = os;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getConfig()
{
return config;
}
// method 3
public int getCost()
{
return cost;
}
// method 4
public String getOs()
{
return os;
}
public static void main(String[] args)
{
// creating object using new operator
Computer c1 = new Computer("Apple","i5", 50000, "IOS");
System.out.println("The company name is "+ c1.getName());
System.out.println("The configuration is "+ c1.getConfig());
System.out.println("Its Cost is "+ c1.getCost());
System.out.println("Its operating System "+ c1.getOs());
}
}
输出:
Ravi
示例 2:
Java
// Java program to show the class declaration
// and how to create an instance of this class
// Class Declaration
public class Computer
{
// Instance Variables
String name;
String config;
int cost;
String os;
// Constructor Declaration of Class
public Computer(String name, String config,
int cost, String os)
{
this.name = name;
this.config = config;
this.cost = cost;
this.os = os;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getConfig()
{
return config;
}
// method 3
public int getCost()
{
return cost;
}
// method 4
public String getOs()
{
return os;
}
public static void main(String[] args)
{
// creating object using new operator
Computer c1 = new Computer("Apple","i5", 50000, "IOS");
System.out.println("The company name is "+ c1.getName());
System.out.println("The configuration is "+ c1.getConfig());
System.out.println("Its Cost is "+ c1.getCost());
System.out.println("Its operating System "+ c1.getOs());
}
}
输出:
The company name is Apple
The configuration is i5
Its Cost is 50000
Its operating System IOS