类定义中,有4种常见的成员

类字段、类方法

实例字段、实例方法

5种常见的引用类型: class, interface, array, enumerated, annotation

public class Integer extends Number implements Serializable, Comparable {

// class members go here

}

abstract class 抽象类,

无法被实例化

任何包含一个抽象方法的类都必须是抽象类

final class

该类无法被继承(扩展)

一个类无法同时声明为abstract 和 final

volatile表示类中字段,可能被多个线程同时调用,读取必须从主存到主存,不能从cpu缓存中读取

一般,类中static field都和final一起使用,用于定义常量

类字段:

可以当作全局变量

没有必要赋予初始化值,编译器将会给定初始值(如果没有初始值)

类方法:

可以看成是全局方法

可以使用同一个类中任意的类字段和类方法, 或者其他类中的类方法和类字段

无法使用类中的实例字段和实例方法

实例字段:

是面向对象编程的关键,保存着对象的状态信息,用于区别不同的对象

没有必要赋予初始化值,编译器将会给定初始值(如果没有初始值)

实例方法:

可以使用所有同一个对象实例的字段

可以使用类字段、类方法

对象可以看作是一组状态(通过实例字段)和在此状态下的一组行为(通过实例方法)的集合

this:

通常情况下,不需要显示声明this(当引用对象实例中的成员)

当本地变量或者实例方法中的变量名称和类字段相同时候,需要使用this

类构造函数:

任意类都至少有一个构造函数,用于创建对象后,进行对象初始化

如果源码中没有定义构造函数,javac编译器将创建一个默认的构造函数(没有传参,也没有特殊初始化)

有关名称、声明、编写构造函数:

名称总是和类名称相同;

声明中没有返回值类型

内部为初始化对象实例代码,相当于构造this引用的属性

没有返回值

定义多个构造函数类似于重载方法

构造函数中可以使用this, 调用该类中的其他构造函数

类字段初始化 vs 实例字段初始化

类字段初始化

javac编译器自动生成一个类初始化方法

类字段将在该初始化方法中真正初始化

通常在类首次调用之前(类第一次加载到jvm中),调用类初始化方法

也可以自定义一些类初始化块,它们将会合并到类初始化方法中(自动生成)

// We can draw the outline of a circle using trigonometric functions

// Trigonometry is slow, though, so we precompute a bunch of values

public class TrigCircle {

// Here are our static lookup tables and their own initializers

private static final int NUMPTS = 500;

private static double sines[] = new double[NUMPTS];

private static double cosines[] = new double[NUMPTS];

// Here's a static initializer that fills in the arrays

static {

double x = 0.0;

double delta_x = (Circle.PI/2)/(NUMPTS-1);

for(int i = 0, x = 0.0; i < NUMPTS; i++, x += delta_x) {

sines[i] = Math.sin(x);

cosines[i] = Math.cos(x);

}

}

// The rest of the class is omitted...

}

实例字段初始化

java编译器将自动生成字段的初始化代码,并且将之加入构造函数中

按照实例字段出现的顺序先后,将实例字段添加到构造函数中

如果构造函数1通过this调用构造函数2, 构造函数2中才会实际处理初始化字段

public class SampleClass {

public int len = 10;

public int[] table = new int[len];

public SampleClass() {

for(int i = 0; i < len; i++) table[i] = i;

}

// The rest of the class is omitted...

}

public SampleClass() {

len = 10;

table = new int[len];

for(int i = 0; i < len; i++) table[i] = i;

}

子类和继承:

通过关键字extends实现继承

public class PlaneCircle extends Circle {

// We automatically inherit the fields and methods of Circle,

// so we only have to put the new stuff here.

// New instance fields that store the center point of the circle

private final double cx, cy;

// A new constructor to initialize the new fields

// It uses a special syntax to invoke the Circle() constructor

public PlaneCircle(double r, double x, double y) {

super(r); // Invoke the constructor of the superclass, Circle()

this.cx = x; // Initialize the instance field cx

this.cy = y; // Initialize the instance field cy

}

public double getCentreX() {

return cx;

}

public double getCentreY() {

return cy;

}

// The area() and circumference() methods are inherited from Circle

// A new instance method that checks whether a point is inside the circle

// Note that it uses the inherited instance field r

public boolean isInside(double x, double y) {

double dx = x - cx, dy = y - cy; // Distance from center

double distance = Math.sqrt(dx*dx + dy*dy); // Pythagorean theorem

return (distance < r); // Returns true or false

}

}

final 类:

类描述符final表示该类无法被继承, 例如:java.lang.String

父类,object类,类结构:

一个类只能有一个直接父类

每一个自定义的类都有一个父类,如果类没有被extends显示声明,其父类为java.lang.Object

object类是java中唯一没有父类的类

其他所有类都继承自object类(直接或者间接)

子类构造函数:

想要调用父类的构造函数,调用super()函数

只能在构造函数中调用super()

子类构造函数中,需要先调用super(), 然后再声明本地变量

构造函数链和默认构造函数:

每次创建新的对象实例时候,都会调用构造函数;

每次子类创建新的对象实例时候,都会调用父类的构造函数;

如果子类的构造函数没有this()或者super()调用父类的构造函数,javac编译器将会隐性添加super()没有参数的父类构造函数

若子类调用的父类构造函数没有显示声明,则会产生编译错误

类无法被实例化,如果类中定义了私有构造函数

隐藏父类字段

如果子类中有父类同名的字段,父类的同名字段被隐藏;所以,尽量不使用同名的字段;

隐藏父类字段不仅是实例字段,还有类字段

隐藏父类方法

当子类定义了一个实例方法,该方法和父类的实例方法有相同的名称、返回类型、传参列表,则父类中的实例方法将会被隐藏

实例方法隐藏

public class Car {

public static final double LITRE_PER_100KM = 8.9;

protected double topSpeed;

protected double fuelTankCapacity;

private int doors;

public Car(double topSpeed, double fuelTankCapacity, int doors) {

this.topSpeed = topSpeed;

this.fuelTankCapacity = fuelTankCapacity;

this.doors = doors;

}

public double getTopSpeed() {

return topSpeed;

}

public int getDoors() {

return doors;

}

public double getFuelTankCapacity() {

return fuelTankCapacity;

}

public double range() {

return 100 * fuelTankCapacity / LITRE_PER_100KM;

}

}

public class SportsCar extends Car {

private double efficiency;

public SportsCar(double topSpeed) {

super(topSpeed, 50.0, 2);

if (topSpeed > 200.0) {

efficiency = 200.0 / topSpeed;

} else {

efficiency = 1.0;

}

}

public double getEfficiency() {

return efficiency;

}

@Override

public double range() {

return 100 * fuelTankCapacity * efficiency / LITRE_PER_100KM;

}

}

3.上面的是对象实例方法的负载,类方法有不同的表现,类方法无法被覆盖,但是可以被隐藏

重写不是隐藏:

class A { // Define a class named A

int i = 1; // An instance field

int f() { return i; } // An instance method

static char g() { return 'A'; } // A class method

}

class B extends A { // Define a subclass of A

int i = 2; // Hides field i in class A

int f() { return -i; } // Overrides method f in class A

static char g() { return 'B'; } // Hides class method g() in class A

}

public class OverrideTest {

public static void main(String args[]) {

B b = new B(); // Creates a new object of type B

System.out.println(b.i); // Refers to B.i; prints 2

System.out.println(b.f()); // Refers to B.f(); prints -2

System.out.println(b.g()); // Refers to B.g(); prints B

System.out.println(B.g()); // A better way to invoke B.g()

A a = (A) b; // Casts b to an instance of class A

System.out.println(a.i); // Now refers to A.i; prints 1

System.out.println(a.f()); // Still refers to B.f(); prints -2

System.out.println(a.g()); // Refers to A.g(); prints A

System.out.println(A.g()); // A better way to invoke A.g()

}

}

查找虚拟方法:

假设,Car[]中包含了Car类和其子类SportsCar对象实例,javac编译器在编译时候,无法知道调用的实例方法range()该使用Car类的实例方法还是SportsCar的实例方法;

javac编译器创建字节码,使用虚拟函数寻找在运行时候,当解释器运行字节码时候,解释器将获取适当的range()实例方法,相对于每一个Car[]中的实例对象

C++等语言中,没有虚拟函数寻找机制,它们的替代方案是使用virtual关键字,如果子类被允许重写实例方法,则需要显示声明virtual

调用一个重写方法

class A {

int i = 1; // An instance field hidden by subclass B

int f() { return i; } // An instance method overridden by subclass B

}

class B extends A {

int i; // This field hides i in A

int f() { // This method overrides f() in A

i = super.i + 1; // It can retrieve A.i like this

return super.f() + i; // It can invoke A.f() like this

}

}

java in a nutshell_【笔记】java in a Nutshell 2相关推荐

  1. java jdk 8学习笔记,Java JDK 8学习笔记 PDF_源雷技术空间

    资源名称:Java JDK 8学习笔记 PDF 内容简介: ●本书是作者多年来教学实践经验的总结,汇集了学员在学习课程或认证考试中遇到的概念.操作.应用等问题及解决方案 ●针对Java SE 8新功能 ...

  2. java in a nutshell_《java in a nutshell》的一些学习笔记

    1.java language does not use a local varible that has not been initialized. java语言中,尚未初始化的局部变量不可以被使用 ...

  3. java null转换jason_Java笔记Java常量、变量

    "要成为绝世高手,并非一朝一夕,除非是天生武学奇才,但是这种人-万中无一" --包租婆这道理放在Java语言学习上也一并受用.在编程方面有着天赋异禀的人毕竟是少数,我们大多数人想要 ...

  4. 黑马程序员Java零基础视频教程笔记-Java基础概念

    文章目录 一.注释 二.关键字 三.字面量 四.变量 五.数据存储 六.数据类型 七.标识符 八.键盘录入 一.注释 1. 定义 注释就是对代码进行解释说明的文字 2. 注释的分类 ① 单行注释:// ...

  5. java short to byte_java笔记 Java中byte数组与int,long,short,char间的转换

    package com.util; public class ByteUtil { /** * 转换short为byte * * @param b * @param s * 需要转换的short * ...

  6. java内部类练习题,学习笔记——Java内部类练习题

    1.尝试在方法中编写一个匿名内部类. package com.lzw; public class AnonymityInnerClass { } class OuterClass4{ public O ...

  7. java web getrealpath,[学习笔记]java web ServletContext.getRealPath(String str)方法的使用

    ServletContext.getRealPath(String str)方法的使用 --------------------------------------------------转载---- ...

  8. Java基础篇 学习笔记

    List item Java基础篇 学习笔记 java基础篇 第1章 计算机.程序和java概述 学习笔记 1.1什么是计算机 简单来说:计算机就是 ' 存储 ' 和 ' 处理 ' 数据的电子设备. ...

  9. Java全栈系列笔记

    Java全栈系列笔记 全部文档.项目.源码: github:https://github.com/name365/Blog-Java 码云:https://gitee.com/yang365/Blog ...

  10. Java全国计算机等级考试二级笔记---java部分

    Java全国计算机二级笔记 ##---------java部分 作者 :arsoooo 本内容供要参加java全国二级计算机等级考试的参考 这是我初学java时做题总结的,会有自己乱总结出来的不普遍的 ...

最新文章

  1. Mozilla公布WebVR API标准草案
  2. Building an MFC project for a non-Unicode character set is deprecated
  3. 大规模微服务利器:eBPF + Kubernetes
  4. linux免密登录_Linux SSH免密钥登录总结
  5. linux下搜狗安装目录,Linux安装搜狗输入法
  6. Android学习系列(15)--App列表之游标ListView(索引ListView)
  7. Zdenek Kalal的TLD Tracker(牛啊,学习!)
  8. Windows 2000安装和配置RIS服务
  9. 当半个娱乐圈都在吃自嗨锅,方便面去哪儿了?
  10. vue入门:(class与style绑定)
  11. Linux C编程与Shell编程在开发实用工具方面的相同点总结
  12. STM32开发项目:步进电机驱动库
  13. android内核编译 me525,今天给ME525+刷了Android 4.0系统,很流畅!
  14. STM32多通道DMA—ADC采样
  15. Hyperautomation for the enhancement of automation in industries 论文翻译
  16. 微信小程序:WeUI一个专为微信小程序设计的UI框架
  17. 如何在网上轻松赚钱,三个非常靠谱的副业项目,一定要收藏起来看
  18. 二叉树交换左右子树的递归与非递归算法
  19. 在条形码上如何添加日期
  20. 输入一个整数,判断是偶数还是奇数

热门文章

  1. 微信小程序の再体验 - - 常用组件
  2. Linux用户密码信息存储目录,Linux用户目录
  3. painter X Gouache(水粉笔)画笔
  4. 监管新规将迫使三星人寿抛售价值160亿美元三星电子股票
  5. 题解-超级英雄Hero
  6. 口腔ct重建服务器原理,牙科CT是什么?牙科CT的原理及优势介绍
  7. 【每日一题】牛客--扑克牌大小
  8. CyberC 2019 征稿 第11届网络分布式计算与知识发现国际会议
  9. SQLServer数据库mdf文件中了勒索病毒firex3m,扩展名变为mdf.firex3m
  10. 对话王学宗:畅谈区块链如何赋能实体?