JAVA 对象和类

参考:

JAVA对象和类 | 菜鸟教程

JAVA 词汇英文版 | Oracle

JAVA Object and Class|Geeksforgeeks

耗时一个星期把JAVA的基础知识过了一遍,因为之前接触过python的原因,个人感觉上手JAVA还是蛮快的,但有一些东西比如JAVA中经常提到的对象、类和一些方法,函数,类的创建方法还是笔记模糊,这次直接在简书上面再复习一遍,下周开始进行“JAVA面向对象”模块的学习。因为之前一直在美国上学,对英语版解释有莫名的好感,因此以下配上英文注释方便理解,有些段落暂时没有翻译,等以后有空了会逐步来完善

定义:

对象: 对象是类的一个实例,有状态和行为。

Object:The principal building blocks of object-oriented programs. Each object is a programming unit consisting of data ( instance variables ) and functionality ( [instance methods](https://www.oracle.com/java/technologies/glossary.html#imethod) ).

对象的组成为:状态,行为,属性。

An object consists of
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other objects.

Objecet

对象声明(Declaring Objects/Instantiating a class)
当一个类的对象被创建时,这个过程为称为将类实例化,所有的实例共用一个类下面的状态和属性。但这些属性的值(状态)对于每个对象都是唯一的,一个类可以拥有任意数量的实例。

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.

public class Puppy{
    int puppyAge;
    public Puppy(String name){
          System.out.println("小狗的名字是:" + name); //实例化对象
    }

初始化/创建对象(Initializing an object)
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

创建对象的方法(ways to create object of class):

对象是根据类创建的。在Java中,使用关键字 new 来创建一个新的对象。创建对象需要以下三步:

声明:声明一个对象,包括对象名称和对象类型。
实例化:使用关键字 new 来创建一个对象。
初始化:使用 new 创建对象时,会调用构造方法初始化对象。

//creating new object of a class
public class Puppy{
   public Puppy(String name){  //声明
      //这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
   public static void main(String[] args){
      // 下面的语句将创建一个Puppy对象
      Puppy myPuppy = new Puppy( "tommy" ); //初始化 次数Puppy为constructor构造器
   }
}

访问实例变量和方法

/* 实例化对象 */
Object referenceVariable = new Constructor();
/* 访问类中的变量 */
referenceVariable.variableName;
/* 访问类中的方法 */
referenceVariable.methodName();

JAVA Constructor|JAVA Point

Defination:
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.

  1. It is a special type of method which is used to initialize the object.
  2. Every time an object is created using the new() keyword, at least one constructor is called.
  3. It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
  4. There are two types of constructors in Java: no-arg constructor, and parameterized constructor. JAVA中存在两种构造器

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.

There are two rules defined for the constructor.

Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized


:类是一个模板,它描述一类对象的行为和状态。

Class:A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.

In general, class declarations can include these components, in order:

  1. Modifiers: A class can be public or has default access (Refer this for details).
  2. class keyword: Class keyword is used to create a class.
  3. Class name: The name should begin with an initial letter (capitalized by convention).
  4. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  6. Body: The class body surrounded by braces, { }.
//创建一个简单的类来理解以下JAVA中类的定义:
public class Dog {  //此处Dog为一个类
    String breed;  //声明成员变量
    int size; 
    String colour; 
    int age; 
 
    void eat() { //方法
    }
 
    void run() { 
    }
 
    void sleep(){ 
    }
 
    void name(){ 
    }
}
类和对象的关系图
类和对象的关系图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容