javase part4 - 面向对象

2017-11-13  本文已影响0人  不再饿肚子给人送饭
Object is everything.
一个简单的类的UML表示.png
面向对象几大概念:(封装,继承,多态) (类,对象,属性,方法)

封装
继承
多态

class className{
  [class body]
}
/**
 * 实体类 Book
 *
 * @author Panda
 */
public class Book {
    //定义属性
    private Integer bookId;
    private String bookName;
    private String bookAuthor;
    private Integer bookPrice;

    //定义方法,方法是类的行为,当前这个方法的行为就是将类中的属性打印到屏幕
    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookAuthor='" + bookAuthor + '\'' +
                ", bookPrice=" + bookPrice +
                '}';
    }
}

对象

属性

方法

returnType methodName(listOfArguments){
  [method body]
}
其余部分

构造器

constructorName(listOfArguements){
  [constructorBody]
}

默认值

实例变量和局部变量

this关键字


 /**
 * JavaBean
 * @author Panda
 */
public class Box {
    int length;
    int width;
    int height;

    /**
     * 构造方法
     * @param length        参数
     * @param width         参数
     * @param height        参数
     */
    public Box(int length, int width, int height) {
        //使用this.length 表示引用的是类的实例变量
        //参数length和表达式右侧的length是同一变量
        this.length = length;
        this.width = width;
        this.height = height;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读