Day05_java_面向对象

package oop;

public class demo01 {
    public static void main(String[] args) {
        /*
        修饰符 返回值类型   方法名(....){
            方法体
            return 返回值
        }
        */

        //return 结束方法,返回一个结果
    }
    public String sayHello(){
package oop;

public class demo02 {
    public static void main(String[] args) {
        //静态方法可以直接文件名点方法调用
        Student.sing();

        //非静态方法
        Student student = new Student();    //new demo().var+回车
        student.say();
    }
}
/*输出的结果是:
学生唱歌
学生说了
*/
View Code

 

return "hello world";
    }

    public void print(){
        return;
    }

    public int max(int a,int b){
        return a>b ? a : b;//三元运算 如果a>b ,则结果是a,如果不是则是b
    }
}

静态方法的调用

demo02调用同级目录的Student文件的方法的两种方式

package oop;

public class demo02 {
    public static void main(String[] args) {
        //静态方法可以直接文件名点方法调用
        Student.sing();

        //非静态方法
        Student student = new Student();    //new demo().var+回车
        student.say();
    }
}
/*输出的结果是:
学生唱歌
学生说了
*/
View Code
package oop;

public class Student {

    //非静态方法
    public void say(){
        System.out.println("学生说了");
    }

    //静态方法有static修饰符
    public static void sing(){
        System.out.println("学生唱歌");
    }
}
View Code

定义add方法

package oop;

public class demo04 {
    public static void main(String[] args) {
        int add = demo04.add(1,2);
        System.out.println(add);        //输出结果是3
    }

    public static int add(int a,int b){
        return a+b;
    }
}
View Code

值传递

package oop;

public class 值传递 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);      //输出的结果是1

        值传递.change(a);
        System.out.println(a);      //输出的结果是1
    }

    //返回值为空
    public static void change(int a){
        a = 10;
    }
}
View Code

引用传递

package oop;

public class 引用传递 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);        //输出null

        引用传递.change(person);
        System.out.println(person.name);        //输出玉华
    }

    public static void change(Person person){
        //persion是一个对象:指向的-->  Person person = new Person();这是一个具体的人,可以改变属性
        person.name = "玉华";
    }

}
class  Person{
    String name;    //null

}
View Code

面向对象编程的本质就是:以类的方式组织代码,以对象的组织封装数据。

构造方法

◆1.必须和类的名字相同,没有返回值

◆2.必须没有返回类型也不能写voi

作用

1、new本质在调用构造方法

2、初始化对象的值

注意点

1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造

 
package oop.demo02;

public class Person {

    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器

    String name;
    int age;

    //实列化初始值
    //1.使用new关键字,本质是在调用构造器
    public Person(){
        this.name = "陈玉华";
    }

    //使用alt+insert键可以快速输入构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
View Code

继承

 

重写

多态

练习:instanceof

 System.out.println(x instanceof y); 判断x 和 y 是否有继承关系,有返回true 否 false

练习:static

package oop.demo09;

public class Student {
    private static int age;//静态的变量    多线程
    private double score;

    public static void main(String[] args) {
        Student student = new Student();

        System.out.println(Student.age);    //静态方法能被所有类共享调用
//        System.out.println(Student.score);      //非静态方法直接调用会报错,
        System.out.println(student.score);  //实列化后才能调用
        System.out.println(student.age);
    }
}
View Code
package oop.demo09;

public class Person {
    //2、附初始值,跟对象同时生成
    {
        System.out.println("匿名代码块");
    }
    //1、只执行一次
    static {
        System.out.println("静态代码块");
    }

    public Person() {
        System.out.println("构成代码块");
    }

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("=============");
        Person person1 = new Person();
    }
}
C:\Users\Administrator\.jdks\openjdk-14.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar=1981:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\IdeaProjects\javaSe\out\production\基础语法 oop.demo09.Person
静态代码块
匿名代码块
构成代码块
=============
匿名代码块
构成代码块

进程已结束,退出代码 0
View Code
package oop.demo09;
import static java.lang.Math.random;//静态导入包
import static java.lang.Math.PI;
public class test {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }
}
C:\Users\Administrator\.jdks\openjdk-14.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar=2281:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\IdeaProjects\javaSe\out\production\基础语法 oop.demo09.test
0.39124547109276
3.141592653589793

进程已结束,退出代码 0
View Code

抽象类

package oop.demo10;
//abstract 抽象类
public abstract class Action {
    //约束——有人帮我们实现
    //abstract ,抽象方法,只有方法名字,没有方法的实现
    public static void main(String[] args) {
    //1、不能new这个抽象类,只能靠子类去实现它,约束
    //2、抽象类中可以写普通的方法
    //3、抽象方法必须在抽象类中
    //存在的意义:抽象出来,提高开发效率
    }

}
View Code
package oop.demo10;
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法
public class A extends Action {
}
View Code

接口

◆普通类:只有具体实现 ◆抽象类:具体实现和规范(抽象方法)都有! ◆接口:只有规范!自己无法写方法~专业的约束!约束和实现分离:面向接口编程

package oop.demo11;
//interface 定义的关键字,接口都需要有实现类
public interface UserService {
    //接口中的所有定义其实都是抽象的public abstract

    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
/*
作用:
1.约束
2.定义一些方法,让不同的人实现~ 10 --->  1
3. public abstract
4. public static final
5.接口不能被实例化~,接口中没有构造方法~
6. implements可以实现多个接口
7.必须要重写接口中的方法~
总结博客
*/
View Code
package oop.demo11;
//轴向类:extends
//类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口的方法  快捷键 ctrl+i
//多继承,利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}
View Code
package oop.demo11;

public interface TimeService {
    void timer();
}
View Code

内部类

◆内部类就是在一个类的内部在定义一个类,比如,A类中定义一个B类,那么B类相对A类来说就称为内部类,而A类相对B类来说就是外部类了。

◆1.成员内部类

◆2.静态内部类

◆3.局部内部类

◆4.匿名内部类

package oop.demo12;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.out();
        Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.getID();
    }
}
View Code
package oop.demo12;

public class Outer {
    private int id=10;
    public void out(){
        System.out.println("这是外部的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部的方法");
        }

        //获得外部内的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}
View Code

异常处理

package oop.demo13;

public class Try {
    public static void main(String[] args) {
        int a=1;
        int b=0;
//       快捷键ctrl+w选中System.out.println(a/b);快捷键ctrl+alt+t选择try/catch/finally即可
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            System.out.println("噢哦,程序错了");
        } finally {
            System.out.println("关闭程序了,不用谢我");
        }
        //finally 可以不要finally,假设IO 资源,关闭


    }
}
View Code
package oop.demo13;

public class Test2 {
    public static void main(String[] args) {
        try {
            new Test2().test(1,0);
        } catch (ArithmeticException e) {       //捕获异常让程序走完
            e.printStackTrace();
        } finally {
        }
    }

    public void test(int a ,int b) throws ArithmeticException{
        if (b==0){
            throw new ArithmeticException();//主动抛出异常,一般在方法中使用
        }
    }
}
View Code

练习:自定义异常

package oop.demo13;

public class MyException extends Exception{
    //传递数字>10,抛出异常
    private int detail;

    public MyException( int a) {
        this.detail = a;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + detail + '}';
    }
}
View Code
package oop.demo13;

public class Application {
    //可能会存在异常的方法
    static void test(int a) throws MyException {
        System.out.println("转递的参数为:"+a);
        if (a>10){
            throw new MyException(a);//抛出
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(1);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }
    }
}
View Code

实际应用中的经验总结

◆处理运行时异常时,釆用逻辑去合理规避同时辅助try- catch处理

◆在多重 catch块后面,可以加一个 catch( Exception)来处理可能会被遗漏的异常

◆对于不确定的代码,也可以加上try- catch,处理潜在的异常

◆尽量去处理异常,切忌只是简单地调用 printStackTrace0去打印输出

◆具体如何处理异常,要根据不同的业务需求和异常类型去决定

◆尽量添加 finallyi语句块去释放占用的资源

posted on 2020-08-28 16:06  cron501  阅读(90)  评论(0)    收藏  举报

导航