java入门教程2

面向对象

java的核心思想就是OOP(面向对象编程)

对于描述复杂的事务,为了从宏观上把控,从整体上合理分析,我们需要使用面向对象的思路分析整个系统,但是,具体到每个微观操作仍需要使用面向过程思路去处理

本质:以类的方式组织代码,以对象的组织封装数据

类和对象的创建

一个类只包含两种对象:属性和方法

类的创建:

public class Student {
    /**属性*/
    int age;
    String name;
    String id;
    /**方法*/
    public void information(){
        System.out.println(this.name+"的学号是:"+this.id+",年龄是:"+this.age);
    }
}

类的调用、对象的创建和调用:

public class Demo01 {
    public static void main(String[] args) {
        //new一个名为student的对象
        Student student = new Student();
        //给对象student初始化
        student.name="胡靖";
        student.id="21130124";
        student.age=18;
        //执行方法
        student.information();
    }
}

构造器

一个类即使什么也不写,也会存在一个方法(构造方法),该方法名与类名相同且没有返回值

new一个对象会先执行一遍构造器内容

作用:初始化值

构造器创建:

public class Person {
    String name;
    /**无参构造*/
    public Person(){
        this.name="胡靖";
    }
    /**有参构造*/
    public Person(String name){
        this.name=name;
    }
}

构造器调用:

public class Demo02 {
    public static void main(String[] args) {
        //调用无参构造器
        Person person = new Person();
        //调用有参构造器
        Person person1 = new Person("韩京");
        //输出两种构造器构造的对象的name属性
        System.out.println(person.name);
        System.out.println(person1.name);

    }
}

快速创建构造器:Alt+Insert

封装

关键字:private

原则:高内聚,低耦合;属性私有,get/set

作用:

  1. 提高程序安全性,保护数据
  2. 隐藏实现细节
  3. 统一接口
  4. 提高系统可维护性

快捷键:Alt+Insert

封装创建:

public class Student {
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age>120||age<0){
            System.out.println("年龄有误");
        }else {
            this.age = age;
        }
    }
}

封装使用:

public static void main(String[] args) {
    Student student = new Student();
    student.setAge(999);
    student.setAge(18);
    System.out.println(student.getAge());;

}

继承

关键字:extends

子类会继承父类所有public的属性和方法

java中所以类都继承object类

父类创建:

public class Person {
    public String name;
    public void say(){
        System.out.println("说了句话");
    }
}

子类创建:

public class Student extends Person{}

调用:

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

快捷键:Ctrl+H:打开继承树

super

关键字:super

this——调用此类;super——调用父类

super调用不了private类型的

子类构造器中有一行访问父类构造器的函数super(),且必须在第一行

父类构造器有参,子类调用的时候就要调用有参构造器

无参父类构造器:

public class Person {
    public Person(){
        
    }
}

无参子类构造器:

public class Student extends Person{
public Student(){
    super();
}
}

有参父类构造器:

public class Person {
    public Person(String name){

    }
}

有参子类构造器:

public class Student extends Person{
public Student(){
    super("name1");
}
}

方法重写

需要有继承关系,子类重写父类

方法:

  1. 方法名必须相同
  2. 参数列表相同

用法:

  1. 子类与父类方法上有差异使需要方法重写

快捷键:Alt+insert

多态

同一方法根据发送对象的不同而采取多种不同的行为方式

一点对象的实际类型是确定的

例如:

//类    对象名 = new 对象
Student s1 = new Student();//new后为对象,学生既是学生
Person s2 = new Student();//也是人
Object s3 = new Student();//也是object

对象能指向哪些方法主要看左边,具体怎么执行看右边

instanceof

测试代码:

public class Person{}
public class Student extends Person{}
public class Teacher extends Person{}
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
Person s4 = new Person();
Object s5 = new Person();
Object s6 = new Object();
System.out.println(s1 instanceof Student);
System.out.println(s1 instanceof Person);
System.out.println(s1 instanceof Object);
System.out.println(s2 instanceof Student);
System.out.println(s2 instanceof Person);
System.out.println(s2 instanceof Object);
System.out.println(s3 instanceof Student);
System.out.println(s3 instanceof Person);
System.out.println(s3 instanceof Object);
//以上均为true
//System.out.println(s1 instanceof Teacher);编译错误
//以下均为false
System.out.println(s2 instanceof Teacher);
System.out.println(s3 instanceof Teacher);
System.out.println(s4 instanceof Teacher);
System.out.println(s5 instanceof Teacher);
System.out.println(s6 instanceof Teacher);

类型转换

public class Person{}//新建类Person
public class Student extends Person{
  public void go(){
        System.out.println("go");
    }
}//新建类Student,继承Person

Person s2 = new Student();

//s2.go;报错
((Student)s2).go();//类型转换

抽象类

关键字:abstract

public abstract class Action {
    /**抽象方法,只有名字,具体实现由子类重写*/
    public abstract void doSomeThing();
}
public class A extends Action{
    @Override
    public void doSomeThing() {
        System.out.println("重写抽象类");
    }
}
  1. 不能new这个抽象类,只能由子类去实现

  2. 抽象类中可以有普通方法

  3. 抽象方法必须在抽象类中

接口

实现接口的类就要重写接口中所以方法

作用:约束

接口创建:

public interface TimerService {
    void timer();
}
public interface UserService {
    void add();
    void delete();
    void change();
    void cheak();
}
public class UserServiceImpl implements UserService,TimerService{
    @Override
    public void add() {
    }
    @Override
    public void delete() {
    }
    @Override
    public void change() {
    }
    @Override
    public void cheak() {
    }
    @Override
    public void timer() {
    }
}

内部类

成员内部类:

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

新建对象:

public static void main(String[] args) {
    Outer outer = new Outer();
    Outer.Inner inner = outer.new Inner();
}

局部内部类:

public class Outer {
    void out() {
        class In{
          
    }
}

匿名内部类:

public class Demo01 {
    public static void main(String[] args) {
        new Apple();
    }
}
class Apple{
    void eat(){
        System.out.println("1");
    }
}

异常exception

异常是指程序运行中出现的意料之外的问题,如文件找不到,无网络连接,参数异常等等

三种类型异常:

  1. 检查性异常:最具代表的是用户错误引起的异常,如打开一个不存在的文件
  2. 运行时异常:可能被程序员避免的异常
  3. 错误:错误不是异常,是脱离程序员控制的问题

捕获、抛出异常

  1. 是什么:在程序出现目标异常时,通过try-catch语句对异常进行捕获并进行一系列自定义操作

  2. 使用场景:需要对异常进行处理时

  3. 用法:

    关键词try、catch、finally

    int a=1;
    int b=0;
    try {
        System.out.println(a/b);
    }catch (RuntimeException r){//捕获到Runtimeexception类型异常时,执行括号内内容
        System.out.println("RuntimeException");
    }catch (Exception e){//捕获到Exception类型异常时,执行括号内内容
        System.out.println("Exception");
    }catch (Throwable t){//捕获到Throwable类型异常时,执行括号内内容
        System.out.println("Throwable");
    }finally {//无论是否捕获到异常,最后都会执行finally中内容
        System.out.println("finally");
    }
    

    关键词throw(手动抛出异常)

    int age;
    age=-999;
    System.out.println(age);
    //以上程序人为看来错误,因为年龄不能小于零,但是程序只看做普通的整形赋值,因此需要人为抛出异常
    if(age<0){
    Exception e = new Exception();//创建新的异常
    throw e;//抛出异常
    }
    

    关键词throws

    fun() throws Exception1,Exception2,Exception3{}
    //表示fun方法可能会抛出三种异常Exception1,Exception2,Exception3,因此在使用时要小心,如可以像下面那样
    try
    {
    fun();
    }catch(Exception1 e1)
    {
    }catch(Exception2 e2)
    {
    }
    

  4. 快捷键:Ctrl+Alt+t

自定义异常

  1. 是什么:自己写的异常

  2. 使用场景:java内置异常无法满足自己需要时

  3. 用法:

    1. 创建自定义异常
    public class MyException extends Exception{//新建类并继承Exception
        public MyException(){
            super();
        }
        public MyException(String msg){
            super(msg);
        }
    }
    
    1. 使用自定义异常
    Demo01 demo01 = new Demo01();
            int a;
            a=10;
            try {
                demo01.fun(a);
            } catch (MyException e) {
                System.out.println("a>1");
            }
    public void fun(int a) throws MyException {
            MyException myException = new MyException();
            if (a>1) {
                throw myException;
            }
        }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值