生活生产中有些数据是不可变的,作成常量java
使用final修饰的变量成为常量,不可再次赋值小程序
final修饰的变量必须在声明的后面直接赋值框架
字面值常量ide
1/3/555aaa、hhh、hehehe学习
自定义常量测试
把变量使用final修饰this
final int area = 960spa
命名自定义常量的时候字母所有大写,多个单词使用下划线链接设计
package com.qf.final0;
public class Demo01 {
public static void main(String[] args) {
double pi = Math.PI;
System.out.println(pi);
double pI2 = MathUtil.PI;
System.out.println(pI2);
// 这个圆周率能够被任意修改,不合理,作成常量不可修改
// MathUtil.PI = 6.666;
double pI3 = MathUtil.PI;
System.out.println(pI3);
}
}
class MathUtil{
// 生活生产中有些数据是不可变的,作成常量
public static final double PI = 3.141592653589793;
}
引用地址不能够改变对象
对象中的内容能够改变
被final修饰的类成为最终的类
这个类不能被继承,没有子类
final修饰的方法成为最终的方法
能够被所在类的子类继承使用
不能被重写
package com.qf.final0;
public class Demo05 {
public static void main(String[] args) {
double pi = Math.PI;
String str = "heiheihei";
System.out.println();
Students s01 = new Students();
// 子类能够继承到final修饰的方法
s01.bitCard();
}
}
final class Calculator{
public static int getSum(int...is) {
int sum = 0;
for (int i = 0; i < is.length; i++) {
sum += is[i];
}
return sum;
}
}
// 最终的类没法被继承,没有子类
// class Calculator000 extends Calculator{}
class Student{
String name;
int age;
// 被final修饰的方法能被继承,不能被重写
public final void bitCard() {
System.out.println("咱们须要使用小程序在校区附近打卡....");
}
}
class Students extends Student{
/*
public final void bitCard() {
System.out.println("咱们须要使用小程序在校区附近打卡....");
}
*/
}
在java中使用interface声明的内容成为接口
接口中的变量只能是公开静态常量
接口中的方法只能是公开抽象方法
接口不能直接建立对象
接口和测试类
package com.qf.inter;
public class Demo01 {
public static void main(String[] args) {
String field = MyInterface.FIELD;
System.out.println(field);
// 能使用类名直接调用说明是static修饰
String yourField = YourInterface.YOUR_FIELD;
System.out.println(yourField);
// 不能从新赋值说明是final修饰
// YourInterface.YOUR_FIELD = "嘿嘿嘿";
}
}
interface MyInterface{
// 属性:公开、静态、常量
public static final String FIELD = "接口中的FIELD";
// 方法:公开、抽象方法
public abstract void show();
}
interface YourInterface{
String YOUR_FIELD = "没有修饰符的FIELD";
}
验证public
package com.qf.inter;
public interface Demo02 {
String OUR_FIELD = "Demo02中的OUR_FIELD";
}
package com.qf.final0;
import com.qf.inter.Demo02;
public class Demo06 {
public static void main(String[] args) {
// 在其余包中也能访问到,说明默认修饰符是public
String ourField = Demo02.OUR_FIELD;
System.out.println(ourField);
}
}
能够生产字节码文件
能够声明为引用类型
不能直接建立对象
拥有Object中全部的方法
接口中全部的属性都是公开、静态、常量,默认使用public static final修饰
接口中的方法都是公开、抽象方法,默认使用public abstract修饰
接口中没有构造方法、动态代码块、静态代码块
接口不能直接建立对象
可让一个类实现接口,重写接口中的方法
类能够在继承其余类的同时,实现多个接口
而后建立这个类的实例,使用重写以后的方法
package com.qf.inter;
public class Demo03 {
public static void main(String[] args) {
ACER acer = new ACER();
acer.power();
acer.transfer();
System.out.println("=================");
USB usb = new ACER();
usb.power();
usb.transfer();
}
}
/**
* USB 接口
* 定义了属性电压和版本
* 定义了方法充电和传输数据
* @author Dushine2008
*
*/
interface USB{
// 属性
double VOLTAGE = 5.0;
double VERSION = 3.0;
// 方法
public abstract void power();
public abstract void transfer();
}
/**
* 子类宏碁电脑
* 实现接口USB
* @author Dushine2008
*
*/
class ACER implements USB{
接口能够用做变量的声明
声明接口的类型,引用指向实现接口的类建立的对象
这个是向上转型的多态
package com.qf.inter;
public class Demo04 {
public static void main(String[] args) {
/**
* Dog类
* 继承Animal
* 实现Runnable接口
* 实现Swiming接口
*/
Dog dog = new Dog("道哥", 2);
dog.run();
dog.swim();
dog.eat();
dog.sleep();
System.out.println("================================");
Animal animal = new Dog();
animal.eat();
animal.sleep();
System.out.println("================================");
Runnable runnable = new Dog();
runnable.run();
System.out.println("================================");
Swimable swimable = new Dog();
swimable.swim();
}
}
/**
* 动物类
* 定义了属性和方法
* @author Dushine2008
*
*/
class Animal{
String name;
int age;
public Animal() {
super();
}
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("不少动物都是杂食性的...");
}
public void sleep() {
System.out.println("大部分动物晚上睡觉...");
}
}
/**
* 能奔跑的接口
* @author Dushine2008
*
*/
interface Runnable{
void run();
}
/**
* 能游泳的接口
* @author Dushine2008
*
*/
interface Swimable{
void swim();
}
/**
* Dog类
* 继承了Animal,得到了Animal中全部非私有的属性和方法
* 实现了Swimable接口,重写了游泳的方法
* 实现了Runnable接口,重写了奔跑的方法
* @author Dushine2008
*
*/
class Dog extends Animal implements Runnable,Swimable{
public Dog() {
super();
}
public Dog(String name, int age) {
super(name, age);
}
/**
* 来自Swimable中的方法
*/
@Override
public void swim() {
System.out.println("狗子天生就会游泳,狗刨这个姿式来来自狗子...");
}
@Override
public void run() {
System.out.println("狗子奔跑的时候用四条腿,速度比人要快一些....");
}
}
把声明的父类引用转换回原来的类型
package com.qf.inter;
public class Demo05 {
public static void main(String[] args) {
/**
* Dog类
* 继承Animal
* 实现Runnable接口
* 实现Swiming接口
*/
Dog5 dog = new Dog5("道哥", 2);
dog.run();
dog.swim();
dog.eat();
dog.sleep();
System.out.println("================================");
Animal5 animal = new Dog5();
animal.eat();
animal.sleep();
Dog5 dog5 = (Dog5) animal;
dog5.run();
dog5.swim();
dog5.eat();
dog5.sleep();
System.out.println("================================");
Runnable5 runnable = new Dog5();
runnable.run();
Dog5 dog05 = (Dog5) runnable;
dog05.run();
dog05.swim();
dog05.eat();
dog05.sleep();
System.out.println("================================");
Swimable5 swimable = new Dog5();
swimable.swim();
// Runnable r = (Runnable) swimable;
}
}
/**
* 动物类
* 定义了属性和方法
* @author Dushine2008
*
*/