对java类的理解

Java Classes and Objects

java基础两个要素:
class 和object

先来看总览介绍:

Java Classes/Objects
Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

在这里插入图片描述
看了图片后写上一段代码:

这个代码一个目标:创建Main class

//Create a class named "Main" with a variable x:

public class Main {
    int x=5;
}

创建完class,创建object
目标:
1、在Main class中加入main fuction
2、在main fuction中创建 myObject
3、输出x
代码如下

//Create a class named "Main" with a variable x:

import java.util.Map;

public class Main {
    int x=5;
//主函数
    public static void main(String[] args) {

      Main  myObj =new Main();
        System.out.println(myObj.x);
        //println()输出int
    }
}

多创建几个Java objects

//Create a class named "Main" with a variable x:

import java.util.Map;

public class Main {
    int x=5;

    public static void main(String[] args) {
      Main  myObj =new Main();
      Main  myObj1= new Main();
      Main  myObj2= new Main();
        System.out.println(myObj.x);
    }
}

多创建几个class,class里面的属性相互使用

You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory/folder:

Main.java
Second.java
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值