java基础篇
IDEA常用快捷键
快捷键功能
Alt+Enter自动修正代码Ctrl+Y删除光标所在行Ctrl+D复制光标所在行的内容,插入光标位置下面Ctrl+Alt +L格式化代码Ctrl+/单行注释,再按取消注释Ctrl+Shift+/选中代码注释,多行注释,再按取消注释。Alt+Ins自动生成代码,toString,get,set等方法Alt+Shift+上下箭头移动当前代码行
接口的定义
接口的定义基本格式
package Demo33
;
public class Interface {
}
接口的抽象方法定义和使用
package Demo33
;
public interface MyInterfaceAbstract {
public abstract void methodAbs1();
abstract void methodAbs2();
public void methodAbs3();
void methodAbs4();
}
使用
package Demo33
;
public class Interface {
public static void main(String
[] args
) {
MyInterfaceAbstractImpl impl
= new MyInterfaceAbstractImpl();
impl
.methodAbs1();
impl
.methodAbs2();
}
}
接口的默认方法定义和使用
package Demo33
;
public class Demo02Interface {
public static void main(String
[] args
) {
MyInterfaceDefaultA a
= new MyInterfaceDefaultA();
a
.methodAbs();
a
.methodDefault();
System
.out
.println("========");
MyInterfaceDefaultB b
= new MyInterfaceDefaultB();
b
.methodAbs();
b
.methodDefault();
}
}
抽象方法
package Demo33
;
public interface MyInterfaceDefault {
public abstract void methodAbs();
public default void methodDefault() {
System
.out
.println("这是新添加的默认方法");
}
}
抽象方法A
package Demo33
;
public class MyInterfaceDefaultA implements MyInterfaceDefault{
@Override
public void methodAbs() {
System
.out
.println("实现了抽象方法,AAA");
}
}
抽象方法B
package Demo33
;
public class MyInterfaceDefaultB implements MyInterfaceDefault{
@Override
public void methodAbs() {
System
.out
.println("实现了抽象方法,BBB");
}
@Override
public void methodDefault() {
System
.out
.println("实现类B覆盖重写了接口的默认方法");
}
}
接口的静态方法定义和使用
package Static
.MyInter
;
public class Demo01Interface {
public static void main(String
[] args
) {
MyInterfaceStatic
.methodStatic();
}
}
接口的静态方法
package Static
.MyInter
;
public interface MyInterfaceStatic {
public static void methodStatic(){
System
.out
.println("这是接口的静态方法");
}
}
是一个类实现一个接口用的关键字:implements
package Static
.MyInter
;
public class MyInterfaceStaticImpl implements MyInterfaceStatic{
}
接口的私有方法定义和使用
package Static
.privateA
;
public interface MyInterfacePrivateA {
public default void methodDefault1() {
System
.out
.println("默认方法1");
}
public default void methodDefault2() {
System
.out
.println("默认方法2");
}
}
MyInterfacePrivateA页面
package Static
.privateA
;
public class MyInterfacePrivateAImpl implements MyInterfacePrivateA {
public void methodAnother() {
}
}
MyInterfacePrivateB页面
package Static
.privateA
;
public interface MyInterfacePrivateB {
public static void methodStatic1() {
System
.out
.println("静态方法1");
}
public static void methodStatic2() {
System
.out
.println("静态方法2");
}
}
Demo01Interface页面
package Static
.privateA
;
public class Demo01Interface {
public static void main(String
[] args
) {
MyInterfacePrivateB
.methodStatic1();
MyInterfacePrivateB
.methodStatic2();
}
}
接口的常量定义和使用
package Const
;
public class Demo01Interface {
public static void main(String
[] args
) {
System
.out
.println(MyInterfaceConst
.NUM_OF_MY_CLASS
);
}
}
package Const
;
public interface MyInterfaceConst {
public static final int NUM_OF_MY_CLASS
= 10;
}
接口的内容小结
继承父类并实现多个接口
package Demo01
;
public class Demo01Interface {
public static void main(String
[] args
) {
Zi zi
= new Zi();
zi
.method();
}
}
实现类
package Demo01
;
public abstract class MyInterfaceAbstract implements MyInterfaceA,MyInterfaceB
{
@Override
public void methodA() {
}
@Override
public void methodAbs() {
}
@Override
public void methodDefault() {
}
}
接口
package Demo01
;
public interface MyInterface {
public default void method() {
System
.out
.println("接口的默认方法");
}
}
MyInterfaceImpl类
package Demo01
;
public class MyInterfaceImpl implements MyInterfaceA,MyInterfaceB
{
@Override
public void methodA() {
System
.out
.println("覆盖重写了A方法");
}
@Override
public void methodAbs() {
System
.out
.println("覆盖重写了AB接口都有的抽象方法");
}
@Override
public void methodB() {
System
.out
.println("覆盖重写了B方法");
}
@Override
public void methodDefault() {
System
.out
.println("对多个接口当中冲突的默认方法进行了覆盖重写");
}
}
接口A
package Demo01
;
public interface MyInterfaceA {
public abstract void methodA();
public abstract void methodAbs();
public default void methodDefault() {
System
.out
.println("默认方法AAA");
}
}
接口B
package Demo01
;
public interface MyInterfaceB {
public abstract void methodB();
public abstract void methodAbs();
public default void methodDefault() {
System
.out
.println("默认方法BBB");
}
}
父类
package Demo01
;
public class Fu {
public void method() {
System
.out
.println("父类方法");
}
}
子类
package Demo01
;
public class Zi extends Fu implements MyInterface{
}
接口之间的多继承
package Demo01
.study
;
public class Demo01Relations {
}
package Demo01
.study
;
public class MyInterfaceImpl implements MyInterface{
@Override
public void method() {
}
@Override
public void methodA() {
}
@Override
public void methodB() {
}
@Override
public void methodCommon() {
}
}
子接口
package Demo01
.study
;
public interface MyInterface extends MyInterfaceA,MyInterfaceB
{
public abstract void method();
@Override
default void methodDefault() {
}
}
接口A
package Demo01
.study
;
public interface MyInterfaceA {
public abstract void methodA();
public abstract void methodCommon();
public default void methodDefault() {
System
.out
.println("AAA");
}
}
接口B
package Demo01
.study
;
public interface MyInterfaceB {
public abstract void methodB();
public abstract void methodCommon();
public default void methodDefault() {
System
.out
.println("BBB");
}
}
多态的概述
多态的格式与使用
package Multi
;
public class Demo01Multi {
public static void main(String
[] args
) {
Fu obj
= new Zi();
obj
.method();
obj
.methodFu();
}
}
父类
package Multi
;
public class Fu {
public void method() {
System
.out
.println("父类方法");
}
public void methodFu() {
System
.out
.println("父类特有方法");
}
}
子类
package Multi
;
public class Zi extends Fu{
@Override
public void method() {
System
.out
.println("子类方法");
}
}
多态中成员变量和成员方法的使用特点
运行代码
package Multi
.Demo02
;
public class MultiMethod {
public static void main(String
[] args
) {
Fu obj
= new Zi();
obj
.method();
obj
.methodFu();
}
}
成员变量不能进行覆盖重写,只有成员方法可以覆盖重写
package Multi
.Demo02
;
public class MultiField {
public static void main(String
[] args
) {
Fu obj
= new Zi();
System
.out
.println(obj
.num
);
System
.out
.println("==========");
obj
.showNum();
}
}
父类
package Multi
.Demo02
;
public class Fu {
int num
= 10;
public void showNum() {
System
.out
.println(num
);
}
public void method() {
System
.out
.println("父类方法");
}
public void methodFu() {
System
.out
.println("父类特有方法");
}
}
子类
package Multi
.Demo02
;
public class Zi extends Fu{
int num
= 20;
@Override
public void showNum() {
System
.out
.println(num
);
}
@Override
public void method() {
System
.out
.println("子类方法");
}
public void methodZi() {
System
.out
.println("子类特有方法");
}
}
使用多态的好处
对象的向上、向下转型(instanceof类型判断)
代码运行
package obj
.Animal
.demo01
.demo1
.animal
;
public class Demo01Main {
public static void main(String
[] args
) {
Animal animal
= new Cat();
animal
.eat();
Cat cat
= (Cat
) animal
;
cat
.catchMouse();
}
}
动物类
package obj
.Animal
.demo01
.demo1
.animal
;
public abstract class Animal {
public abstract void eat();
}
猫类
package obj
.Animal
.demo01
.demo1
.animal
;
public class Cat extends Animal{
@Override
public void eat() {
System
.out
.println("猫吃鱼");
}
public void catchMouse() {
System
.out
.println("猫抓老鼠");
}
}
狗类
package obj
.Animal
.demo01
.demo1
.animal
;
public class Dog extends Animal{
@Override
public void eat() {
System
.out
.println("狗吃骨头");
}
public void watchHouse() {
System
.out
.println("狗看家");
}
}
用instanceof关键字进行类型判断
package obj
.Animal
.demo01
.demo1
.animal
;
public class Demo02Instanceof {
public static void main(String
[] args
) {
Animal animal
= new Cat();
animal
.eat();
if (animal
instanceof Dog) {
Dog dog
= (Dog
) animal
;
dog
.watchHouse();
}
if (animal
instanceof Cat) {
Cat cat
= (Cat
) animal
;
cat
.catchMouse();
}
giveMeAPet(new Dog());
}
public static void giveMeAPet(Animal animal
) {
if (animal
instanceof Dog) {
Dog dog
= (Dog
) animal
;
dog
.watchHouse();
}
if (animal
instanceof Cat) {
Cat cat
= (Cat
) animal
;
cat
.catchMouse();
}
}
}
多态综合案例:笔记本电脑接口
接口的基本使用–对象的上下转型–使用接口作为方法的参数
package obj
.Animal
.demo01
.demo1
.USB
;
public class DemoMain {
public static void main(String
[] args
) {
Computer computer
= new Computer();
computer
.powerOn();
USB usbMouse
= new Mouse();
computer
.useDevice(usbMouse
);
Keyboard keyboard
= new Keyboard();
computer
.useDevice(keyboard
);
computer
.useDevice(new Keyboard());
computer
.powerOff();
System
.out
.println("============");
method(10.0);
method(20);
int a
= 30;
method(a
);
computer
.powerOff();
}
public static void method(double num
) {
System
.out
.println(num
);
}
}
电脑
package obj
.Animal
.demo01
.demo1
.USB
;
public class Computer {
public void powerOn() {
System
.out
.println("笔记本电脑开机");
}
public void powerOff() {
System
.out
.println("笔记本电脑关机");
}
public void useDevice(USB usb
) {
usb
.open();
if (usb
instanceof Mouse) {
Mouse mouse
= (Mouse
) usb
;
mouse
.click();
} else if (usb
instanceof Keyboard) {
Keyboard keyboard
= (Keyboard
) usb
;
keyboard
.type();
}
usb
.close();
}
}
usb 抽象类
package obj
.Animal
.demo01
.demo1
.USB
;
public abstract class USB {
public abstract void open();
public abstract void close();
}
鼠标
package obj
.Animal
.demo01
.demo1
.USB
;
public class Mouse extends USB {
@Override
public void open() {
System
.out
.println("打开鼠标");
}
@Override
public void close() {
System
.out
.println("关闭鼠标");
}
public void click() {
System
.out
.println("鼠标点击");
}
}
键盘
package obj
.Animal
.demo01
.demo1
.USB
;
public class Keyboard extends USB {
@Override
public void open() {
System
.out
.println("打开键盘");
}
@Override
public void close() {
System
.out
.println("关闭键盘");
}
public void type() {
System
.out
.println("键盘输入");
}
}
final关键字
final关键字-修饰类
package obj
.Animal
.demo01
.demo1
.Final
;
public final class MyClass {
public void method() {
System
.out
.println("方法执行");
}
}
父类:final关键字-修饰成员方法
package obj
.Animal
.demo01
.demo1
.Final
;
public abstract class Fu {
public final void method() {
System
.out
.println("父类方法执行");
}
public abstract void methodAbs();
}
子类
package obj
.Animal
.demo01
.demo1
.Final
;
public class Zi extends Fu{
@Override
public void methodAbs() {
}
}
final关键字-修饰局部变量
package obj
.Animal
.demo01
.demo1
.Final
;
public class Demo01Final {
public static void main(String
[] args
) {
int num1
= 10;
System
.out
.println(num1
);
num1
= 20;
System
.out
.println(num1
);
final int num2
= 200;
System
.out
.println(num2
);
final int num3
;
num3
= 30;
Student stu1
= new Student("赵丽颖");
System
.out
.println(stu1
);
System
.out
.println(stu1
.getName());
stu1
= new Student("霍建华");
System
.out
.println(stu1
);
System
.out
.println(stu1
.getName());
System
.out
.println("=======================");
final Student stu2
= new Student("刘诗诗");
System
.out
.println(stu2
.getName());
stu2
.setName("诗爷");
System
.out
.println(stu2
.getName());
}
}
Person类:final关键字-修饰成员变量
package obj
.Animal
.demo01
.demo1
.Final
;
public class Person {
private final String name
;
public Person() {
name
= "刘诗诗";
}
public Person(String name
) {
this.name
= name
;
}
public String
getName() {
return name
;
}
}
学生类
package obj
.Animal
.demo01
.demo1
.Final
;
public class Student {
private String name
;
public Student() {
}
public Student(String name
) {
this.name
= name
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
}
四种权限修饰符
Java中有四种权限修饰符: (权限:从大到小)
public > protected > (default) > private
注意:(default) 并不是关键字"default",而是根本不写。
-publicprotected(default)private
同一个类(我自己)√√√√同一个包(我邻居)√√√×不同包子类(我儿子)√√××不同包非子类(陌生人)√×××
成员内部类的定义和使用
package wd01
.demo01
;
public class Demo01InnerClass {
public static void main(String
[] args
) {
Body body
= new Body();
body
.methodBody();
System
.out
.println("========");
Body
.Heart heart
= new Body().new Heart();
heart
.beat();
}
}
package wd01
.demo01
;
public class Body {
private String name
;
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public class Heart {
public void beat() {
System
.out
.println("心脏跳动");
System
.out
.println("我叫:" + name
);
}
}
public void methodBody() {
System
.out
.println("外部类的方法");
Heart heart
= new Heart();
heart
.beat();
}
}
内部类的同名变量访问
package wd01
.demo01
;
public class Demo02InnerClass {
public static void main(String
[] args
) {
Outer
.Inner obj
= new Outer().new Inner();
obj
.methodInner();
}
}
package wd01
.demo01
;
public class Outer {
int num
= 10;
public class Inner {
int num
= 20;
public void methodInner() {
int num
= 30;
System
.out
.println(num
);
System
.out
.println(this.num
);
System
.out
.println(Outer
.this.num
);
}
}
}
局部内部类的final问题
package wd01
.demo01
.Outes
;
public class MyOuter {
public void methodOuter() {
final int num
= 10;
class MyInner {
public void methodInner() {
System
.out
.println(num
);
}
}
}
}
匿名内部类【重点】
package wd01
.demo01
.niName
;
public class DemoMain {
public static void main(String
[] args
) {
MyInterface objA
= new MyInterface() {
@Override
public void method() {
System
.out
.println("匿名内部类实现了方法");
}
};
objA
.method();
System
.out
.println("===========");
new MyInterface() {
@Override
public void method() {
System
.out
.println("匿名内部类实现了方法");
}
}.method();
}
}
package wd01
.demo01
.niName
;
public interface MyInterface {
public abstract void method();
}
类作为成员变量类型
package wd01
.demo01
.Heros
;
public class DemoMain {
public static void main(String
[] args
) {
Hero hero
= new Hero();
hero
.setName("盖伦");
hero
.setAge(20);
Weapon weapon
= new Weapon("圣剑");
hero
.setWeapon(weapon
);
hero
.attack();
}
}
英雄
package wd01
.demo01
.Heros
;
public class Hero {
private String name
;
private int age
;
private Weapon weapon
;
public Hero() {
}
public Hero(String name
, int age
, Weapon weapon
) {
this.name
= name
;
this.age
= age
;
this.weapon
= weapon
;
}
public void attack() {
System
.out
.println("年龄为" + age
+ "的" + name
+ "用" + weapon
.getCode() + "攻击敌方");
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
public Weapon
getWeapon() {
return weapon
;
}
public void setWeapon(Weapon weapon
) {
this.weapon
= weapon
;
}
}
武器
package wd01
.demo01
.Heros
;
public class Weapon {
private String code
;
public Weapon() {
}
public Weapon(String code
) {
this.code
= code
;
}
public String
getCode() {
return code
;
}
public void setCode(String code
) {
this.code
= code
;
}
}
接口作为成员变量类型
package wd01
.demo01
.implement
;
public class DemoGame {
public static void main(String
[] args
) {
Hero hero
= new Hero();
hero
.setName("艾希");
hero
.setSkill(new SkillImpl());
Skill skill
= new Skill() {
@Override
public void use() {
System
.out
.println("呯呯呯");
}
};
hero
.setSkill(skill
);
hero
.setSkill(new Skill() {
@Override
public void use() {
System
.out
.println("喵喵喵~");
}
});
hero
.attack();
}
}
package wd01
.demo01
.implement
;
public class Hero {
private String name
;
private Skill skill
;
public Hero() {
}
public Hero(String name
, Skill skill
) {
this.name
= name
;
this.skill
= skill
;
}
public void attack() {
System
.out
.println("我叫" + name
+ "开始释放技能");
skill
.use();
System
.out
.println("释放技能完成");
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public Skill
getSkill() {
return skill
;
}
public void setSkill(Skill skill
) {
this.skill
= skill
;
}
}
package wd01
.demo01
.implement
;
public class SkillImpl implements Skill{
@Override
public void use() {
System
.out
.println("咚咚咚~");
}
}
package wd01
.demo01
.implement
;
public interface Skill {
void use();
}
接口作为方法的参数和返回值
package wd01
.demo01
.implement
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class DemoInterface {
public static void main(String
[] args
) {
List
<String> list
= new ArrayList<>();
List
<String> result
= addNames(list
);
for (int i
= 0; i
< result
.size(); i
++) {
System
.out
.println(result
.get(i
));
}
}
public static List
<String> addNames(List
<String> list
) {
list
.add("迪丽热巴");
list
.add("古力娜扎");
list
.add("马尔扎哈");
list
.add("沙扬娜拉");
return list
;
}
}