对java类的理解
Posted 咸鱼加辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对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
以上是关于对java类的理解的主要内容,如果未能解决你的问题,请参考以下文章