[java基础]-面向对象_2
每一个java类
都是一个.java
文件
多态
动态编译
//Application
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s2.run();
}
}
//Person
public class Person {
public void run(){
System.out.println("Person");
}
}
//Student
public class Student extends Person{
}
输出
修改代码 重写 run()
方法
//Application
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s2.run();
s1.run();
}
}
//Student
public class Student extends Person{
@Override
public void run() {
System.out.println("Student");
}
}
运行输出
s2.run()//子类重写父类的方法,执行子类的方法
添加 eat()
方法
//Student
public class Student extends Person{
@Override
public void run() {
System.out.println("Student");
}
public void eat(){
System.out.println("eat");
}
}
//Application
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s2.eat(); //报错
}
}
Person s2 = new Student();
s2.eat();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
Student s1 = new Student();
//Student 能调用的方法都是自己的或者继承父类的
Person s2 = new Student();
//Person 父类型,可以指向子类,但不能调用子类独有的方法
Object s3 = new Student();
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类有联系,类型转换异常! ClassCastException!
3.存在条件:继承关系,方法需要重写,父类的引用指向子类对象! Father f1 = new Son();
Instanceof 类型转换
//Application
public class Application {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student); //True
System.out.println(object instanceof Person); //True
System.out.println(object instanceof Object); //True
System.out.println(object instanceof Teacher); //False
System.out.println(object instanceof String); //False
}
}
//Person.java
public class Person {
}
//Teacher.java
public class Teacher extends Person{
}
//Student.java
public class Student extends Person{
}
运行得到
因为
Object > Person > Student
Object > Person > Teacher
Object > String
继续尝试
//Application
public class Application {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student); //True
System.out.println(object instanceof Person); //True
System.out.println(object instanceof Object); //True
System.out.println(object instanceof Teacher); //False
System.out.println(object instanceof String); //False
System.out.println("=======");
Person person = new Student();
System.out.println(person instanceof Student); //True
System.out.println(person instanceof Person); //True
System.out.println(person instanceof Object); //True
System.out.println(person instanceof Teacher); //False
// System.out.println(person instanceof String); //报错
System.out.println("=======");
Student student = new Student();
System.out.println(student instanceof Student); //True
System.out.println(student instanceof Person); //True
System.out.println(student instanceof Object); //True
// System.out.println(student instanceof Teacher); //报错
// System.out.println(student instanceof String); //报错
}
}
System.out.println(X instanceof Y);
X和 Y 有关系
转换类型
//Application
public class Application {
public static void main(String[] args) {
//类型之间的转换
//高 低
Person Muz1 = new Student();
//将这个对象转换为Student类型,我们可以使用Student类的方法
Student muz1 = (Student) Muz1;
muz1.go();
((Student) Muz1).go();
}
}
//Student
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
运行
Student muz1 = (Student) Muz1;
muz1.go();
((Student) Muz1).go();
//两种转换类型的格式
static
静态的属性
//Demo
public class Student {
private static int age; //静态变量
private double score; //非静态变量
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age); //类
System.out.println(s1.age); //对象
}
}
静态的方法
//Demo
public class Student {
public void run(){
System.out.println("run");
}
public static void go(){
System.out.println("go");
}
public static void main(String[] args) {
new Student().run(); //非静态方法
go(); //静态方法
}
}
//run
//go
静态代码块
public class Person {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
}
}
输出
main方法
中添加代码
public static void main(String[] args) {
Person person = new Person();
System.out.println("=====");
Person person1 = new Person();
}
执行输出
static{}静态方法 只执行一次
抽象类
abstract
//Action
//abstract 抽象类
public abstract class Action {
//abstract,抽象方法,只有方法的名字,没有方法的实现
public abstract void doSomething();
}
//A
//抽象类对的所有方法,继承了他的子类,都必须要实现它的方法
public class A extends Action{
@Override
//方法的重写
public void doSomething() {
}
}
类是单继承,接口可以多继承
新建一个main方法
去new
抽象类
//Application
public class Application {
public static void main(String[] args) {
new Action();
}
}
只能去new
抽象类的子类
接口
关键字 interface
//Demo
public interface UserService {
}
接口中不存在方法
只有抽象的定义
public interface UserService {
//接口中的所有定义其实都是抽象的
void go();
public abstract void run();
}
写一个增删查改方法
//UserService
public interface UserService {
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
写一个接口,必须要重写方法
//抽象类:extends
//接口 implements 接口
public class UserServiceImpl implements UserService{
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
}
作用:
1.约束
2.定义一些方法,让不同的人实
3.public abstra
4.public static
5.接口不被实例化,接口中没有
6.implements可以实
7.必须要重写接口中的方法
内部类
Demo
//Out
public class Outer {
private int id;
public void out(){
System.out.println("out");
}
public class Inner{
public void in(){
System.out.println("in");
}
}
}
//Application
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
}
}
内部类获取外部类的资源 私有属性
//Out
public class Outer {
private int id = 2022;
public class Inner{
public void getID(){
System.out.println(id);
}
}
}
//Application
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}
一个java类
中可以有多个class类
,但只能有一个public class类
public class Outer {
}
class A{
}
class B{
}
没有名字初始化类
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用实例保存
new Apple().eat();
}
}
class Apple{
public void eat(){
System.out.println("Apple");
}
}
Error和Exception
检查性异常
运行时异常
错误ERROR
捕获和抛出异常
try
catch
finally
throw
throws
Demo写一段报错代码
//Test
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
System.out.println(a/b);
}
}
出现异常,然后实现捕获异常
//Test
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a/b);
}catch (ArithmeticException e){
System.out.println("程序出现异常,变量b不能为0");
}finally {
System.out.println("finally");
}
}
}
finally
都会执行
手动栈溢出
//Test
public class Test {
public static void main(String[] args) {
try { //监控区域
new Test().a();
}catch (Throwable e){ //捕获异常
System.out.println("程序出现异常,捕获到Throwable");
}finally { //最后执行
System.out.println("finally");
}
}
public void a(){
b();
}
public void b() {
a();
}
}
捕获 1/0 的异常等级
//Test
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try { //监控区域
System.out.println(a/b);
} catch (Error e){ //捕获异常
System.out.println("Error");
} catch (Exception e){
System.out.println("Exception");
} catch (Throwable t){
System.out.println("Throwable");
} finally { //最后执行
System.out.println("finally");
}
}
}
逐层递进关系 , 从小到大捕获
主动抛出异常
//Test
public class Test {
public static void main(String[] args) {
new Test().test(1,0);
}
public void test(int a, int b){
if(b == 0){
throw new ArithmeticException();//主动抛出异常
}
}
}