Java教程

Java 对象和类

在Java中有3种初始化对象的方法。
通过参考变量 通过方法 通过构造函数

1)对象和类示例: 通过引用进行初始化

初始化对象意味着将数据存储到对象中。让我们看一个简单的示例,在该示例中,我们将通过引用变量来初始化对象。
文件: TestStudent2.java
class Student{
    int id;
    String name;
}
class TestStudent2{
    public static void main(String args[]){
        Student s1=new Student();
        s1.id=101;
        s1.name="Sonoo";
        System.out.println(s1.id+" "+s1.name);
    //printing members with a white space }
}
输出:
101 Sonoo
我们还可以创建多个对象,并通过引用变量在其中存储信息。
文件: TestStudent3.java
class Student{
    int id;
    String name;
}
class TestStudent3{
    public static void main(String args[]){
        //Creating objects Student s1=new Student();
        Student s2=new Student();
        //Initializing objects s1.id=101;
        s1.name="Sonoo";
        s2.id=102;
        s2.name="Amit";
        //Printing data System.out.println(s1.id+" "+s1.name);
        System.out.println(s2.id+" "+s2.name);
    }
}
输出:
101 Sonoo102 Amit

2)对象和类示例: 通过方法进行初始化

在此示例中,我们将创建Student的两个对象类,并通过调用insertRecord方法初始化这些对象的值。在这里,我们通过调用displayInformation()方法来显示对象的状态(数据)。
文件: TestStudent4.java
class Student{
    int rollno;
    String name;
    void insertRecord(int r, String n){
        rollno=r;
        name=n;
    }
    void displayInformation(){
        System.out.println(rollno+" "+name);
    }
}
class TestStudent4{
    public static void main(String args[]){
        Student s1=new Student();
        Student s2=new Student();
        s1.insertRecord(111,"Karan");
        s2.insertRecord(222,"Aryan");
        s1.displayInformation();
        s2.displayInformation();
    }
}
输出:
111 Karan222 Aryan
Java中具有值的对象
如上图所示,对象获取堆内存中的内存区。引用变量引用在堆内存区域中分配的对象。此处,s1和s2都是引用变量,它们引用内存中分配的对象。

3)对象和类示例: 通过构造函数进行初始化

稍后我们将学习Java中的构造函数。

对象和类示例: 员工

让我们看一个我们维护员工记录的示例。
文件: TestEmployee.java
class Employee{
    int id;
    String name;
    float salary;
    void insert(int i, String n, float s) {
        id=i;
        name=n;
        salary=s;
    }
    void display(){
        System.out.println(id+" "+name+" "+salary);
    }
}
public class TestEmployee {
    public static void main(String[] args) {
        Employee e1=new Employee();
        Employee e2=new Employee();
        Employee e3=new Employee();
        e1.insert(101,"ajeet",45000);
        e2.insert(102,"irfan",25000);
        e3.insert(103,"nakul",55000);
        e1.display();
        e2.display();
        e3.display();
    }
}
输出:
101 ajeet 45000.0102 irfan 25000.0103 nakul 55000.0

对象和类示例: Rectangle

给出了另一个维护Rectangle类记录的示例。
文件: TestRectangle1.java
class Rectangle{
    int length;
    int width;
    void insert(int l, int w){
        length=l;
        width=w;
    }
    void calculateArea(){
        System.out.println(length*width);
    }
}
class TestRectangle1{
    public static void main(String args[]){
        Rectangle r1=new Rectangle();
        Rectangle r2=new Rectangle();
        r1.insert(11,5);
        r2.insert(3,15);
        r1.calculateArea();
        r2.calculateArea();
    }
}
输出:
55 45

在Java中创建对象的方法有哪些?

在Java中创建对象的方法有很多。他们是:
使用new关键字 通过newInstance()方法 通过clone()方法 通过反序列化 通过工厂方法等
我们稍后将学习这些创建对象的方法。
使用Java创建对象的不同方式

匿名对象

匿名只是表示无名。没有引用的对象称为匿名对象。只能在创建对象时使用它。
如果您只需要使用一次对象,则匿名对象是一种很好的方法。例如:
new Calculation();
//anonymous object
通过引用调用方法:
Calculation c=new Calculation();
c.fact(5);
通过匿名对象调用方法
new Calculation().fact(5);
让我们看一下Java中匿名对象的完整示例。
class Calculation{
    void fact(int n){
        int fact=1;
        for(int i=1;i<=n;i++){
            fact=fact*i;
        }
        System.out.println("factorial is "+fact);
    }
    public static void main(String args[]){
        new Calculation().fact(5);
    //calling method with anonymous object}
}
输出:
Factorial is 120

仅按一种类型创建多个对象

我们只能按一种类型创建多个对象,就像在以下情况下一样
原始变量的初始化:
int a=10, b=20;
引用变量的初始化:
Rectangle r1=new Rectangle(), r2=new Rectangle();
//creating two objects
让我们看一下示例:
//Java Program to illustrate the use of Rectangle class which
//has length and width data membersclass Rectangle{
    int length;
    int width;
    void insert(int l,int w){
        length=l;
        width=w;
    }
    void calculateArea(){
        System.out.println(length*width);
    }
}
class TestRectangle2{
    public static void main(String args[]){
        Rectangle r1=new Rectangle(),r2=new Rectangle();
        //creating two objects r1.insert(11,5);
        r2.insert(3,15);
        r1.calculateArea();
        r2.calculateArea();
    }
}
输出:
55 45

真实示例: TestAccount

文件: TestAccount.java
//Java Program to demonstrate the working of a banking-system
//where we deposit and withdraw amount from our account.
//Creating an Account class which has deposit() and withdraw() methodsclass Account{
    int acc_no;
    String name;
    float amount;
    //Method to initialize objectvoid insert(int a,String n,float amt){
        acc_no=a;
        name=n;
        amount=amt;
    }
    //deposit methodvoid deposit(float amt){
        amount=amount+amt;
        System.out.println(amt+" deposited");
    }
    //withdraw methodvoid withdraw(float amt){
        if(amount<amt){
            System.out.println("Insufficient Balance");
        }
        else{
            amount=amount-amt;
            System.out.println(amt+" withdrawn");
        }
    }
    //method to check the balance of the accountvoid checkBalance(){
        System.out.println("Balance is: "+amount);
    }
    //method to display the values of an objectvoid display(){
        System.out.println(acc_no+" "+name+" "+amount);
    }
}
//Creating a test class to deposit and withdraw amountclass TestAccount{
    public static void main(String[] args){
        Account a1=new Account();
        a1.insert(832345,"Ankit",1000);
        a1.display();
        a1.checkBalance();
        a1.deposit(40000);
        a1.checkBalance();
        a1.withdraw(15000);
        a1.checkBalance();
    }
}
输出:
832345 Ankit 1000.0Balance is: 1000.040000.0 depositedBalance is: 41000.015000.0 withdrawnBalance is: 26000.0
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4