Home > Java > javaTutorial > body text

JAVA tutorial | Objects and classes

黄舟
Release: 2017-02-25 09:39:27
Original
1430 people have browsed it

Java Objects and Classes


##JavaAs a Object-oriented language. The following basic concepts are supported:


    • #Inheritance

    • ##Encapsulation

    • Abstract

    • #Class

    • Object

    • ##Instance
    • Method
    • Overload
##In this section we focus on studying the concepts of


objects and classes

:

    Object:
    • The object is an instance of the class (The object is not to find a girlfriend ), has state and behavior. For example, a dog is an object, its status includes: color, name, breed; behaviors include: wagging its tail, barking, eating, etc.

      Class:
    • A class is a template that describes the behavior and status of a type of object.

      Objects in Java

Now Let’s take a closer look at what an object is. If you look at the real world around you, you will find that there are many objects around you, such as cars, dogs, people, etc. All these objects have their own state and behavior.


Take a dog as an example. Its status includes: name, breed, and color. Its behaviors include: barking, wagging its tail, and running.


Comparing real objects and software objects, they are very similar.


#Software objects also have state and behavior. The state of a software object is its attribute, and its behavior is reflected through methods.


#In software development, methods operate on changes in the internal state of objects, and mutual calls between objects are also completed through methods.


public class Dog{
   String breed; 
   int age;
   String color;  //颜色  
   public void barking(){
   }  
   public void hungry(){
   }  
   public void sleeping(){
   }
}
Copy after login

A class can contain the following types of variables:

    • Local variables: Variables defined in methods, constructors or statement blocks are called local variables. Variable declaration and initialization are all in methods. After the method ends, the variables will be automatically destroyed.

    • Member variables: Member variables are variables defined in the class and outside the method body. Such variables are instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.

    • Class variables: Class variables are also declared in the class, outside the method body, but they must be declared as static type.



##A class can have multiple methods, in the above In the example: barking(), hungry() and sleeping() are all methods of the Dog class.


Constructor method


Every class has a constructor method. If no constructor is explicitly defined for a class, the Java compiler will provide a default constructor for the class.


When creating an object, at least one constructor must be called. The name of the constructor must be the same as the class. A class can have multiple constructors.


The following is an example of a constructor method:


public class Yang{
    public Yang(){
    }
 
    public Yang(String name){
        // 这个构造器仅有一个参数:name
    }
}
Copy after login


创建对象



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

    • 声明:声明一个对象,包括对象名称和对象类型。

    • 实例化使用关键字new来创建一个对象。

    • 初始化:使用new创建对象时,会调用构造方法初始化对象。



下面是一个创建对象的例子:


public class Puppy{
   public Puppy(String name){
      //这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
   public static void main(String []args){
      // 下面的语句将创建一个Puppy对象
      Puppy myPuppy = new Puppy( "tommy" );
   }
}
Copy after login


编译并运行上面的程序,会打印出下面的结果:

小狗的名字是 : tommy
Copy after login


访问实例变量和方法

通过已创建的对象来访问成员变量和成员方法,如下所示:


/* 实例化对象 */
ObjectReference = new Constructor();
/* 访问其中的变量 */
ObjectReference.variableName;
/* 访问类中的方法 */
ObjectReference.MethodName();
Copy after login



实例



下面的例子展示如何访问实例变量和调用成员方法:


public class Puppy{
   int puppyAge;
   public Puppy(String name){
      // 这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
   
   public void setAge( int age ){//get set
       puppyAge = age;
   }
 
   public int getAge( ){
       System.out.println("小狗的年龄为 : " + puppyAge ); 
       return puppyAge;
   }
 
   public static void main(String []args){
      /* 创建对象 */
      Puppy myPuppy = new Puppy( "tommy" );
      /* 通过方法来设定age */
      myPuppy.setAge( 2 );
      /* 调用另一个方法获取age */
      myPuppy.getAge( );
      /*你也可以像下面这样访问成员变量 */
      System.out.println("变量值 : " + myPuppy.puppyAge ); 
   }
}
Copy after login


编译并运行上面的程序,产生如下结果:

小狗的名字是 : tommy小狗的年龄为 : 2变量值 : 2
Copy after login

源文件声明规则



在本节的最后部分,我们将学习源文件的声明规则。当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则。


    • 一个源文件中只能有一个public类

    • 一个源文件可以有多个非public类

    • 源文件的名称应该和public类的类名保持一致。例如:源文件中public类的类名是Employee,那么源文件应该命名为Employee.java。

    • 如果一个类定义在某个包中,那么package语句应该在源文件的首行。

    • 如果源文件包含import语句,那么应该放在package语句和类定义之间。如果没有package语句,那么import语句应该在源文件中最前面。

    • import语句和package语句对源文件中定义的所有类都有效。在同一源文件中,不能给不同的类不同的包声明。



类有若干种访问级别,并且类也分不同的类型:抽象类和final类等。这些将在访问控制章节介绍。


除了上面提到的几种类型,Java 还有一些特殊的类,如:内部类、匿名类


Java包


包主要用来对类和接口进行分类。当开发Java程序时,可能编写成百上千的类,因此很有必要对类和接口进行分类。


Import语句



在Java中,如果给出一个完整的限定名,包括包名、类名,那么Java编译器就可以很容易地定位到源代码或者类。Import语句就是用来提供一个合理的路径,使得编译器可以找到某个类。


例如,下面的命令行将会命令编译器载入java_installation/java/io路径下的所有类

import java.io.*;
Copy after login




一个简单的例子



在该例子中,我们创建两个类:EmployeeEmployeeTest


首先打开文本编辑器,把下面的代码粘贴进去。注意将文件保存为 Employee.java。


Employee类有四个成员变量:name、age、designation和salary。该类显式声明了一个构造方法,该方法只有一个参数。


import java.io.*;
 
public class Employee{
   String name;
   int age;
   String designation;
   double salary;
   // Employee 类的构造器
   public Employee(String name){
      this.name = name;
   }
   // 设置age的值
   public void empAge(int empAge){
      age =  empAge;
   }
   /* 设置designation的值*/
   public void empDesignation(String empDesig){
      designation = empDesig;
   }
   /* 设置salary的值*/
   public void empSalary(double empSalary){
      salary = empSalary;
   }
   /* 打印信息 */
   public void printEmployee(){
      System.out.println("名字:"+ name );
      System.out.println("年龄:" + age );
      System.out.println("职位:" + designation );
      System.out.println("薪水:" + salary);
   }
}
Copy after login


程序都是从main方法开始执行。为了能运行这个程序,必须包含main方法并且创建一个实例对象。

下面给出EmployeeTest类,该类实例化2个 Employee 类的实例,并调用方法设置变量的值。

将下面的代码保存在 EmployeeTest.java 文件中。


import java.io.*;
public class EmployeeTest{
 
   public static void main(String args[]){
      /* 使用构造器创建两个对象 */
      Employee empOne = new Employee("RUNOOB1");
      Employee empTwo = new Employee("RUNOOB2");
 
      // 调用这两个对象的成员方法
      empOne.empAge(26);
      empOne.empDesignation("高级程序员");
      empOne.empSalary(1000);
      empOne.printEmployee();
 
      empTwo.empAge(21);
      empTwo.empDesignation("菜鸟程序员");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}
Copy after login
编译这两个文件并且运行 EmployeeTest 类,可以看到如下结果:


$ javac-> EmployeeTest.java
$ java-> EmployeeTest 名字:RUNOOB1年龄:26职位:高级程序员薪水:1000.0名字:RUNOOB2年龄:21职位:菜鸟程序员薪水:500.0
Copy after login



 以上就是JAVA 入坑教程 | 对象和类的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!