Java Class & Objects: 类和对象

Class & Objects

Class

  • a set of related attributes & behavior
  • like a template of a thing describeb by attributes & behavior
Attributes
  • the statues of a thing
Behaviors
  • what this thing can do

eg:

class: a cat
attributes: name, weight, age, color
behaviors: walk, run

Object

Class is the description of a type of thing, which is abstract.

Object is an example of a type of thing, which is specific.

Class is the templet of Object, Object is the entity of Class

Attention
  • common method should include keyword: static, but in the method of a member, it is not allowed.
  • member’s variables are defined outside of methods of the class
  • different examples of the same class share the same starting address of behavior method
  • the example of a class can be refered
  • when an object is transmitted into a method, the actual transitted is the address of the object
  • when the tpye of a variable is “boolean”, “Getter Function” should be written as “isXxx”.
  • you can add Class with another Class in different package
  • when a local variable and a member variable in the same Class have the same name in a method, the method will use the closet variable as priority. And you can use this. to represent member variables. this is called by the the particular object by Class.
// Class Declaration  
public class Dog { 
    // Instance Variables 
  	// behaviors
    String name; 
    String breed; 
    int age; 
    String color; 
  
    // Constructor Declaration of Class 
    public Dog(String name, String breed, int age, String color){ 
        this.name = name; 
        this.breed = breed; 
        this.age = age; 
        this.color = color; 
    } 
  	
  	// Attributes
    // method 1 
    public String getName(){ 
        return name; 
    } 
  
    // method 2 
    public String getBreed(){ 
        return breed; 
    } 
  
    // method 3 
    public int getAge(){ 
        return age; 
    } 
  
    // method 4 
    public String getColor(){ 
        return color; 
    } 
  
    @Override
    public String toString(){ 
        return("Hi my name is "+ this.getName()+ 
               ".\nMy breed,age and color are " + 
               this.getBreed()+"," + this.getAge()+ 
               ","+ this.getColor()); 
    } 
  
    public static void main(String[] args){ 
        Dog tuffy = new Dog("tuffy","papillon", 5, "white"); 
        System.out.println(tuffy.toString()); 
    } 
} 

Construction Method

  • the name of a construction method should be completely the same as the Class, even the case.
  • construction method don’t need return and void
  • if you don’t write a construction method manually, the complier will create one automatically. Once you write it, the complier won’t create another any more. But you can write more than one construction method with different argument list – overload of construction.

A Standard Class Consists of Following 4 Parts: (Java Bean)

  1. all member variables are modified by private
  2. all member variables need a pair of Getter&Setter
  3. a construction method without arguments
  4. a construction method with full arguments
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值