当一个类中有抽象方法时,这个类就是抽象类,当他的子类调用时,必须实现父类的抽象方法
抽象类的要点:
1有抽象方法的类只能定义为抽象类
2抽象类不能用new来实例化抽象类
3抽象类可以包含属性,方法,构造器,但是构造方法不能用来new实例,只能被子类调用
4抽象类只能用来被继承
5抽象方法必须被子类实现
为什么用抽象类:定义抽象类后,相当于制定一个规则(必须实现),统一了子类的行为
属性私有后(private),提供相应的get/set方法来访问相关属性,这些方法通常是public修饰的,以提供对属性的赋值于读取操作
public abstract class chouxiang {
int id;
private String name;
abstract public void study();
abstract public void exam();
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
//无参构造器
chouxiang(){
}
}
package com.java.project_05;
public class test {
public static void main(String[] args) {
//chouxiang c=new chouxiang("张三");抽象类不能创建对象
chouxiang c1=new cx();//父类引用指向子类对象
c1.study();
c1.exam();
}
}
class cx extends chouxiang{
@Override
public void study() {
System.out.println("敲代码");
}
@Override
public void exam() {
System.out.println("进大厂");
}
}
JDK8以前
只能定义抽象方法
接口 interface
接口,定义的是一组规则
如果你是。。。就必须。。。
规范和具体实现完全分离
普通类:具体实现
抽象类:具体实现,规范(抽象方法)
接口:规范
接口可以有多个父接口(普通类留下了羡慕的眼泪)
public interface jiekou {
int minhight=100;
/*飞行方法*/
public abstract void fly();
void stop();//public abstract加不加都可以,默认有
}
interface Honest{
void help();
}
public class test_jk implements jiekou,Honest{
@Override
public void fly() {
System.out.println("内裤外穿的飞︿( ̄︶ ̄)︿");
}
@Override
public void stop() {
System.out.println("竖着停");
}
@Override
public void help() {
System.out.println("我来组成头部");
}
public static void main(String[] args) {
jiekou t0=new test_jk();
test_jk t1=new test_jk();
t1.fly();
t0.stop();
t1.help();
//t0.help();不能用
Honest h=(Honest) t0;
((Honest) t0).help();
h.help();
//程序编译的时候只看"jiekou"的类型,你说什么就是什么,但运行的时候,跑的是test_jk的方法
}
}
JDK8以后
允许在接口定义类方法和默认方法
1默认方法(扩展方法):抽象方法必须被实现,默认方法是具体的,有方法体(不需要实现),实现方法为使用default关键字
默认方法只是个特别一点的(需要加default)的普通方法
public class test_moren {
public static void main(String[] args) {
A a=new test_A();
a.moren();
}
}
interface A{
default void moren(){
System.out.println("我是接口A的默认方法");
}
}
class test_A implements A{
@Override
public void moren() {
System.out.println("Test_A.moren");
}
}
2.静态方法:静态方法直接从属于接口,可以通过接口名调用
public class test_jingtai {
public static void main(String[] args) {
B.StaticMethod();//静态方法通过类名直接调用
test_B.staticMethod();
}
}
interface B{
public static void StaticMethod(){
System.out.println("B.staticMethod");
}
}
class test_B implements B{
public static void staticMethod(){
System.out.println("test_B.staticMethod");
}
}
默认方法通过类名直接调用,静态方法通过实现类去调用
静态与默认
package com.java.project_05;
public class test_j_and_m {
public static void main(String[] args) {
C c=new test_C();
c.moren();
}
}
interface C{
//静态方法
public static void staticMethod(){
System.out.println("c.staticMethod");
}
//默认方法
public default void moren(){
staticMethod();
System.out.println("moren ");
}
}
class test_C implements C{
public static void staticMethod(){
System.out.println("test_c.staticMethod");
}
}
多继承
package com.java.project_05;
public class test_jk_duojicheng implements C1 {//只用实现C1,A,B,C,方法都有
@Override
public void testa() {
System.out.println("我是A哒");
}
@Override
public void testb() {
System.out.println("我是B哒");
}
@Override
public void testc() {
System.out.println("我是C哒");
}
public static void main(String[] args) {
test_jk_duojicheng tdjc=new test_jk_duojicheng();
tdjc.testa();
tdjc.testb();
tdjc.testc();
}
}
interface A1{
void testa();
}
interface B1{
void testb();
}
interface C1 extends A1,B1{
void testc();
}
test实现了C1,C1继承了A1,B1
String基础
String类又称作不可变字符序列
位于java.lang包中
被 “” 引起来的字符串都存放在方法区
比较字符串内容是否相等使用qeuals
package com.java.project_05.string;
public class test1 {
public static void main(String[] args) {
String str=new String("abcdefg");//创建String对象
String str2="abcdEFG";
String str3="ef";
/*System.out.println(str1==str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str1.equals(str2));*/
System.out.println(str.charAt(0));//返回字符串第0个字符
System.out.println(str.charAt(6));
System.out.println(str.length());//返回字符串的长度
System.out.println(str.length()-1);
System.out.println(str.equals(str2));//false
System.out.println(str.equalsIgnoreCase(str2));//忽略大小写
System.out.println(str.indexOf(str3));//从头开始查找,返回字符串str3在str中首次被找到的位置
System.out.println(str.lastIndexOf(str3));//从末尾开始查找,返回字符串str3在str中首次被找到的位置
//替换掉选中的字符或字符串
String str5="asdzxcqazwsx".replace("d","j");
String str6="abcdefgh".replace("fgh","xyz");
System.out.println(str5);
System.out.println(str6);
//判断是否已所选字符串开头或结尾
System.out.println("abcd,i love".startsWith("ab"));
System.out.println("abcd,i love".endsWith("ab"));
//截取字符串
System.out.println("0123456789".substring(7));//从第7位开始,到结束
System.out.println("0123456789".substring(7,9));//从第7位开始,到第9位之前,(9-7=2)输出2位
System.out.println("abcDE".toUpperCase());//将字符串中的小写字母改为大写
System.out.println("abcDE".toLowerCase());//将字符串中的大写字母改为小写
System.out.println(" sdfhshf ".replace(" ","*"));
String str7=" sd f s ";
for(int i=0;i<str7.length();i++){
System.out.println(str7.charAt(i));
}
//去除首尾空格
System.out.println(str7.trim());
System.out.println(str7.replace(" ",""));
System.out.println(str7);//旧字符串没有任何变化
}
}
内部类:
定义在一个类内部的类。在类中,我们定义了属性,方法,还可以再定义类
内部类可以直接使用外部类的成员
public class test_1 {
private String j="我不做人啦";
private int age=10;
private void show(){
System.out.println("JOJO");
}
//内部类
public class Inner{
private String i="Tom";
private int age=18;
public void showinner(){
show();
System.out.println(j);
System.out.println(age);
//当内部类与外部类的属性重名时,使用(外部类).this.(属性名)调用
System.out.println(test_1.this.age);
}
}
public static void main(String[] args) {
test_1.Inner i=new test_1().new Inner();
i.showinner();
//调用的是
}
}
内部类的作用:
1.提供了更好的封装,只能让外部类直接访问,不允许同一个包中的其他类直接访问
2.内部类可以直接访问外部类的私有属性,内部类被当成外部类的成员,但外部类不能访问内部类的内部属性
非静态内部类
1.非静态内部类对象必须寄存在一个外部类对象里。 因此,如果有一个非静态内部类
对象那么一定存在对应的外部类对象。非静态内部类对象单独属于外部类的某个对
象。
2.非静态内部类可以直接访问外部类的成员,但是外部类不能直接访问非静态内部类
成员。
3.非静态内部类不能有静态方法、 静态属性和静态初始化块。
4.成员变量访问要点:
1.内部类里方法的局部变量 :变量名。
2.内部类属性 : this.变量名。
3.外部类属性 :外部类名.this变量名。
public class test_2 {
private int id=10;//普通属性
private static int ida=20;//静态属性
static class Inner2{
public void test(){
System.out.println(id);//静态内部类不能访问外部类的普通属性
System.out.println(ida);
}
}
public static void main(String[] args) {
test_2.Inner2 i2=new test_2.new Inner2();
i2.test();
}
}
匿名内部类
package com.java.project_05.neibulei;
//匿名内部类
public class test_3 {
public void test(A a){
a.run();
}
public static void main(String[] args) {
test_3 t4=new test_3();
t4.test(new AA());
}
}
interface A{
void run();
}
class AA implements A{
@Override
public void run() {
System.out.println("AA.test");
}
}
局部内部类
方法内部定义一个类,仅能在该方法中使用