包 (package) 是组织类的一种方式.使用包的主要目的是保证类的唯一性. (一)导入包中的类
导入毫米级别的时间戳 方式一
java.util.Date date=new java.util.Date();
System.out.println(date.getTime());
导入毫米级别的时间戳 方式二: 使用import导入
import java.util.Date;public class text {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date.getTime());
}}
导入毫米级别的时间戳 方式三:
需要使用 java.util 中的其他类, 可以使用import java.util.*;
import java.util.*;public class text1 {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date.getTime());
}}
但是当要使用多个包时,此方法会产生歧义,不建议。
(二)包的访问权限类中的 public 和 private:private 中的成员只能被类的内部使用.成员不包含 public 和 private 关键字, 此时这个成员可以在包内部的其他类使用, 但是不能在包外部的类使用 二、继承 (一)什么是继承
类似于集合中的交集,如动物类中包含name、run等成员属性,猫、狗类中也包含name、run等成员属性,此时,猫、狗类即可继承动物类中的name、run等属性,而不需重复定义,可有效减少代码冗余。
从逻辑上讲, 猫和狗 都是一种 动物 (is - a 语义). (二)语法规则
class 子类 extends 父类 {
}
使用 extends 指定父类.Java 中一个子类只能继承一个父类 (而C++/Python等语言支持多继承).子类会继承父类的所有 public 的字段和方法.对于父类的 private 的字段和方法, 子类中是无法访问的.子类的实例中, 也包含着父类的实例,可以使用super关键字使用父类的实例
class Animal{
public String name;
public Animal(String name){
this.name=name;
}
public void eat(String food){
System.out.println(this.name+"正在吃"+food);
}}class Cat extends Animal{
public Cat(String name){
super(name);
}}class Bird extends Animal{
public Bird(String name){
super(name);
}
public void fly(){
System.out.println(this.name+"正在飞");
}}public class text2 {
public static void main(String[] args) {
Cat cat=new Cat("白");
cat.eat("猫粮");
Bird bird=new Bird("原原");
bird.fly();
}
}
(三)protected关键字
如果把字段设为 private, 子类不能访问. 但是设成 public, 又违背了我们 “封装” 的初衷。两全其美的办法就是 protected 关键字.
对于类的调用者来说, protected 修饰的字段和方法是不能访问的对于类的 子类 和 同一个包的其他类 来说, protected 修饰的字段和方法是可以访问的
class Animal{
protected String name;
public Animal(String name){
this.name=name;
}
public void eat(String food){
System.out.println(this.name+"正在吃"+food);
}
}
1、小结:
private: 类内部能访问, 类外部不能访问默认(也叫包访问权限): 类内部能访问, 同一个包中的类可以访问, 其他类不能访问.protected: 类内部能访问, 子类和同一个包中的类可以访问, 其他类不能访问.public : 类内部和类的调用者都能访问 (四)final关键字
final 关键字, 修饰一个变量或者字段的时候, 表示 常量 (不能修改).final 关键字也能修饰类, 此时表示被修饰的类就不能被继承. 标题三、组合
和继承类似, 组合也是一种表达类之间关系的方式, 也是能够达到代码重用的效果.
public class Student {
...}public class Teacher {
...}public class School {public Student[] students;public Teacher[] teachers;}
组合并没有涉及到特殊的语法(诸如 extends 这样的关键字), 仅仅是将一个类的实例作为另外一个类的字段.这是我们设计类的一种常用方式之一.
组合表示 has - a 语义
父类通常在子类的上方. 所以我们就称为 “向上转型” , 表示往父类的方向转.
1、直接赋值
Bird bird = new Bird("圆圆");Animal bird2 = bird;// 或者写成下面的方式Animal bird2 = new Bird("圆圆")
2、方法传参
public static void main(String[] args) {
Bird bird=new Bird("玉玉");
feed(bird);
}
public static void feed(Animal animal){
animal.eat("谷子");
}
3、方法返回`
public static void main(String[] args) {
Animal animal=findMyAnimal();
}
public static Animal findMyAnimal(){
Bird bird=new Bird("玉玉");
return bird;
}
此时方法 findMyAnimal 返回的是一个 Animal 类型的引用, 但是实际上对应到 Bird 的实例.
(二)向下转型向上转型是子类对象转成父类对象, 向下转型就是父类对象转成子类对象. 相比于向上转型来说, 向下转型没那么常见,但是也有一定的用途.
public static void main(String[] args) {
Animal animal=new Animal("玉玉");
Bird bird=(Bird)animal;
bird.fly();
}
(三)动态绑定
当子类和父类中出现同名方法的时候。
class Animal1{
protected String name;
public Animal1(String name){
this.name=name;
}
public void eat(String food){
System.out.println("我是一只小动物");
System.out.println(this.name+"正在吃"+food);
}}class Bird extends Animal1 {
public Bird(String name){
super(name);
}
public void fly(){
System.out.println(this.name+"正在飞");
}
public void eat(String food){
System.out.println("我是一只小鸟");
System.out.println(this.name+"正在吃"+food);
}}public class text3 {
public static void main(String[] args) {
Animal1 animal1=new Animal1("兔兔");
animal1.eat("谷子");
Animal1 animal12=new Bird("琪琪");
animal12.eat("谷子");
}
}
//执行结果
我是一只小动物
兔兔正在吃谷子
我是一只小鸟
琪琪正在吃谷子
在 Java 中, 调用某个类的方法, 究竟执行了哪段代码 (是父类方法的代码还是子类方法的代码) , 要看究竟这个引用指向的是父类对象还是子类对象. 这个过程是程序运行时决定的(而不是编译期), 因此称为 动态绑定.
(四)方法重写子类实现父类的同名方法, 并且参数的类型和个数完全相同, 这种情况称为 覆写/重写/覆盖(Override).
重写中子类的方法的访问权限不能低于父类的方法访问权限.重写的方法返回值类型不一定和父类的方法相同(但是建议最好写成相同, 特殊情况除外).
1、动态绑定和方法重写
方法重写是 Java 语法层次上的规则, 而动态绑定是方法重写这个语法规则的底层实现. 两者本质上描述的是相同的事情, 只是侧重点不同
前面的代码中由于使用了重写机制, 调用到的是子类的方法. 如果需要在子类内部调用父类方法怎么办? 可以使用super 关键字
1、使用了 super 来调用父类的构造器
public Bird(String name) {super(name);}
2、使用 super 来调用父类的普通方法
public class Bird extends Animal {public Bird(String name) {
super(name);}
public void eat(String food) {super.eat(food);System.out.println("我是一只小鸟");
System.out.println(this.name + "正在吃" + food);
}
}
五、抽象类
(一)什么是抽象类
abstract class Shape{
abstract public void draw();}
在 draw 方法前加上 abstract 关键字, 表示这是一个抽象方法. 同时抽象方法没有方法体(没有 { }, 不能执行具体代码).对于包含抽象方法的类, 必须加上 abstract 关键字表示这是一个抽象类.抽象类不能直接实例化.抽象方法不能是 private抽象类中可以包含其他的非抽象方法, 也可以包含字段. 这个非抽象方法和普通方法的规则都是一样的, 可以被重写,也可以被子类直接调用 (二)抽象类的作用
抽象类存在的最大意义就是为了被继承.
抽象类本身不能被实例化, 要想使用, 只能创建该抽象类的子类. 然后让子类重写抽象类中的抽象方法.
使用抽象类相当于多了一重编译器的校验. 六、接口 (一)语法规则
nterface Ishape{
void draw();}class Cycle implements Ishape{
public void draw(){
System.out.println("○");
}}public class text4 {
public static void main(String[] args) {
Ishape shape=new Cycle();
shape.draw();
}
}
使用 interface 定义一个接口接口中的方法一定是抽象方法, 因此可以省略 abstract接口中的方法一定是 public, 因此可以省略 publicCycle 使用 implements 继承接口. 此时表达的含义不再是 “扩展”, 而是 “实现”在调用的时候同样可以创建一个接口的引用, 对应到一个子类的实例.接口不能单独被实例化.extends:指的是当前已经有一定的功能了, 进一步扩充功能.implements: 实现指的是当前啥都没有, 需要从头构造出来.接口中只能包含抽象方法. 对于字段来说, 接口中只能包含静态常量(final static). (二)多个接口
Java 中只支持单继承, 一个类只能 extends 一个父类. 但是可以同时实现多个接口, 也能达到多继承类似的效果.
如下:
class Animal{
protected String name;
public Animal(String name){
this.name=name;
}}**创建接口:**interface IRunning{
void run();}interface ISwimming{
void swim();}**实现多个接口**class Cat extends Animal implements IRunning{
public Cat(String name){
super(name);
}
public void run(){
System.out.println(this.name+"正在跑");
}}class Fish extends Animal implements ISwimming{
public Fish(String name){
super(name);
}
@Override
public void swim() {
System.out.println(this.name+"正在游");
}}class Duck extends Animal implements ISwimming,IRunning{
public Duck(String name){
super(name);
}
public void run(){
System.out.println(this.name+"正在跑");
}
public void swim(){
System.out.println(this.name+"游泳");
}}
继承表达的含义是 is - a 语义, 而接口表达的含义是 具有 xxx 的特性 (三)接口使用实例
class Student implements Comparable{
private String name;
private int score;
public Student(String name,int score){
this.name=name;
this.score=score;
}
public String toString(){
return "["+this.name+":"+this.score+"]";
}
@Override
public int compareTo(Object o) {
Student s=(Student)o;
if (this.score>s.score){
return -1;
}else if (this.score
(四)接口之间的继承
接口可以继承一个接口, 达到复用的效果. 使用 extends 关键字
interface IRunning {void run();}interface ISwimming {void swim();}// 两栖的动物, 既能跑, 也能游
interface IAmphibious extends IRunning, ISwimming {}class Frog implements IAmphibious {...}
(五)Clonable接口
class Animal implements Cloneable{
private String name;
public Animal clone(){
Animal o=null;
try{
o=(Animal) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}}public class text6 {
public static void main(String[] args) {
Animal animal=new Animal();
Animal animal1=animal.clone();
System.out.println(animal==animal1);
}}



