Chapter 2 Classes and Object

1 篇文章 0 订阅

目录

1. Object

2. Classes

3. Methods

4. Varible

5. Public and Private

6. Static

7. Constructor

8. Accessors, Mutators, Static methods, Static methods in Diver class

9. method overloading

10. instance variable

11. calling methods


1. Object

Def ( big java p25): objects are entities in the program that are manipulated by calling methods.

An object is characterized by its state and behavior.

2. Classes

Def ( barron p93): a class is a software blueprint for implementing objects of a given type.

An object is a single instance of the class

3. Methods

Def ( barron p93): the methods of the class provide both the behaviors exhibited by the object and the operations that manipulate the object.

Combining an object's data and methods into a single unit called a class is known as encapsulation.

4. Varible

Def ( big java p26): a varible is a storage location in a computer program.

5. Public and Private ( barron p94 )

public -- usable for all programs

private -- usable only in its package

6. Static ( barron p94 )

A static variable contains a value that is shared by all instances of the class.

Static -- memory allocation happens once

final -- constant / = the value can not be changed

7. Constructor

参考:Default Constructor in Java – Class Constructor Example

A constructor creates an object of the class.

always have the same name with the class

  • no return type
  • default constructor

no-argument constructor:

public class library {
    String bookname;
    int ISBN;

    //library constructor
    //no-argument constructor
    public library(){
        bookname="Harry Potter";
        ISBN=123;
    }

    public static void main(String args[]){
        library a = new library();
        System.out.println(a.ISBN);
    }
}

输出结果:

parameterized coonstructor:

(1) without this

public class library {
    String bookname;
    int ISBN;

    //library constructor
    //parameterized constructor
    public library(String bookname, int ISBN){
        bookname=bookname;
        ISBN=ISBN;
    }

    public static void main(String args[]){
        library a = new library("Harry Potter", 123);
        System.out.println(a.ISBN);
    }
}

输出结果: 

 (2) with this

public class library {
    String bookname;
    int ISBN;

    //library constructor
    //parameterized constructor
    public library(String bookname, int ISBN){
        this.bookname=bookname;
        this.ISBN=ISBN;
    }

    public static void main(String args[]){
        library a = new library("Harry Potter", 123);
        System.out.println(a.ISBN);
    }
}

 输出结果:

 this is a key word refers to the current object in a method or constructor

8. Accessors, Mutators, Static methods, Static methods in Diver class (barron p96,97)

accessors -- An accessor meMutatorsthod accesses a class object without altering the object.

mutators -- A mutator method changes the state of an object by modifying at least one of its in stance variables.

static methods -- A method that performs an operation for the entire class, not its individual objects.

Static methods in Diver class -- often a class that contains the main() method is used as a driver program to test other classes.

9. method overloading (barron p99) 

overloaded methods are two or more methods in the same class that have the same name but different parameter lists.

10. instance variable (在C++中称之为member variable)

  • 类里面的方法
  • Instance variables are declared in a class, but outside a method, constructor or any block.

11. calling methods

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值