JAVA 面试知识点 4 -- 面向对象编程( OOP)之object和class

面向对象编程(Object-Oriented Programming )概念

Object-Oriented Programming的基本概念是classobject
有四个特性: 继承(Inheritance)多态(Polymorphism)抽象(Abstraction)封装(Encapsulation)
这一章详细讲class 和object, 最后讲抽象和封装。
1.object
2. class(类)
3. 创建object的方法
4. static关键词
5. final关键词
6. 嵌套类(nested class)
7. abstract class(抽象类)
8.Interfaces
9. abstract class 和 interface对比
10. 封装

1. object

现实生活中,object一般包括了属性和行为。 java里对应的就是fields和method。

比方说 一条狗包含了属性 (name, color, breed, hungry) 和行为 (barking, fetching, wagging tail)

For each object that you see, ask yourself two questions: “What possible states can this object be in?” and “What possible behavior can this object perform?”.

object 的好处:

  • 模块化: 关于object的代码可以独立管理,区别于其他object。object创建之后就可以很容易的被传递。
  • 隐藏信息: 把信息藏在object内部,其他object看不到。
  • 代码re-use: object被创建之后可以被到处用。
  • 很容易被插入和移除。 一旦确定一个object出问题,可以很容易被拿掉。
    https://docs.oracle.com/javase/tutorial/java/concepts/object.html

2. class

现实世界中,把同类的object归为一类。比方说

There may be thousands of other bicycles in existence, all of the same make and model.

在oop里,每辆自行车都是一个“自行车类”的实例。

Each bicycle was built from the same set of blueprints and therefore contains the same components.

2.1. class的组成:

  • fields: 一些变量,描述属于这个class的object应该有的一些属性。
    • [access_modifier] [static] [final] type name [= initial value] ; ([]里为可写可不写的)
    • 尽量定义为private的,然后写getter和setter来access这些参数。
  • constructor: 构造函数。 用来实例化一个object。
    • Constructor(s) 的名字必须和类的名字一样。
    • Constructor没有返回值
    • Constructor 不能是abstract, final, static 和 Synchronized.
    • 当用new来实例化一个object的时候,就会调用构造函数。
    • 一个class可以有很多个Constructor,接受不同的参数。new()里面有几个参数就会调用相对应参数的构造函数。
    • 没有参数的Constructor是默认构造函数。在一个class里,如果没有声明任何的Constructor,系统会分配一个默认构造函数 。 如果声明了任何一个构造函数,系统就不会给了。
    • this()方法用来调用自己的构造函数。
  • method: 成员方法。
    • [access_modifier] [final] return_type name(parameter_list)[Exception list ]; ([]里为可写可不写的)
    • Method signature: 包含方法名字和参数。
    • 名字要尽量为动词。描述方法作用。
    • local variables: 局部变量,是定义在方法里面的变量。
    • method的执行是在stack上的,详情在JAVA 面试知识点 2 --空间分配
  • 构造函数(Constructor) vs. 成员方法(method)
    • 构造函数的名字必须和类名一模一样。成员方法名可以和类名一样,但是极度不推荐。
    • 构造函数没有返回值,成员方法必须有返回函数,如果meiyou返回值,则必须声明为void
    • 从object层面来看,构造函数只在实例化object的时候被调用一次。但是可以调用method很多次。

一个基本的class和object 例子(包含上面所有要点):

class Animal {
    //定义一个Animal的class,就是这么简单
	//fields. 每个animal应该有的属性,尽量设置为private。
	private boolean vegetarian;
	private int noOfLegs;
	//default constructor. 默认构造函数(没有参数的构造函数)。
	//如果一个class没有写构造函数,那么系统就会给一个默认的。
	public Animal(){
    
		// Animal a = new Animal()会调用这个constructor。
	    System.out.println("an animal created");
	}
	//每个class可以有很多个不同参数的构造函数,用new创建一个object的时候会根据不同的参数选择构造函数。
	//如果class写了构造函数,不带参数的构造函数就不会被系统添加,如果要用到,需要手动添加。
	// Animal a = new Animal(true, 4)会调用这个constructor。
	public Animal(boolean vegetarian, int legs){
   
		//同一个class里的constructor可以被互相用this调用。
		this(); //this()用来调用自己的其他的构造函数(省代码)
		this.vegetarian = vegetarian;
		this.noOfLegs = legs;
		System.out.println("an animal with " + legs + " legs created");
	}
	//methods 和构造函数不同的是,method的声明必须带返回类型
	public boolean isVegetarian() {
   
		return vegetarian;
	}
	//如果没有返回类型,就写个void
	public void setVegetarian(boolean vegetarian) {
   
		this.vegetarian = vegetarian;
	}
	public int getNoOfLegs() {
   
		return noOfLegs;
	}
	public void setNoOfLegs(int noOfLegs) {
   
		this.noOfLegs = noOfLegs;
	}
	//上面这几个都是getter和setter
	
	public void eat() {
   
		System.out.println("animal can eat");
	}
	public void eat(String food) {
   
		System.out.println("animal can eat" + food);
	}
	//上面两个方法同名不同参数, 叫做overload(请与override区分开来)。
}
public class AnimalTest{
   
     public static void main(String []args){
   //main 方法是程序的入口。
        Animal a = new Animal(true, 4); //创造了一个Animal的object,叫做a.
        a.eat();
        a.eat("onion");
        //通过getter和setter来设置private变量。
        System.out.println(</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值