美文网首页
Java - Day5

Java - Day5

作者: o0寳贝 | 来源:发表于2016-06-28 19:13 被阅读0次

    �继承的实现

    1, 继承的基本概念  : 扩展父类的功能

    2,Java中使用extends关键字完成继承      class 子类 extends 父类{}

    代码:

    class Person{

    private int age;

    private String name;

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public void tel() {

    // System.out.println("姓名"+getName()+" 年龄"+getAge());

    }

    }

    class Student extends Person{

    private int score;

    public int getScore() {

    return score;

    }

    public void setScore(int score) {

    this.score = score;

    }

    public void want() {

    System.out.println("成绩"+getScore()+"姓名"+getName()+" 年龄"+getAge());

    }

    }

    public class ExtendsDemo01 {

    public static void main(String[] args) {

    Student stu = new Student();

    stu.setAge(13);

    stu.setName("zhangsan");

    stu.setScore(100);

    stu.tel();

    继承的限制

    1, 在Java中只允许单继承, 但可以多层集成

    2, 子类不能直接访问父类的私有成员,  需要写setter和getter方法

    子类对象的实例化

    1, 在子类对象实例化之前,必须先调用父类中的构造方法, 之后调用子类构造方法

    代码:

    class Father{

    private int age;

    public Father(){

    System.out.println("父类的构造方法");

    }

    }

    class Son extends Father{

    public Son(){

    System.out.println("子类的构造方法");

    }

    }

    public class ExtendsDemo03 {

    public static void main(String[] args) {

    Son son = new Son();

    } //控制台打印结构: 先:父类的构造方法 再:子类的构造方法

    }

    Java方法重写与super关键字

    方法的重写

    1, 在继承中, 也存在着重写的概念, 其实就是子类定义了和父类同名的方法

    2, 定义:  方法名称相同, 返回值类型相同, 参数也相同

    3, 重写限制:  被子类重写的方法不能拥有比父类方法更加严格的访问权限

    4, 访问权限: private < default < public

    super关键字

    1, super关键字: 强行调用父类的方法的执行

    2, super不一定在重写中使用, 也可以表示那些方法时从父类中继承而来的

    代码:

    class A{

    public void tell() {

    System.out.println("我是父类的tell方法");

    }

    void say(){

    System.out.println("我是父类的say方法");

    }

    }

    class B extends A{

    public void tell() {

    super.tell();

    System.out.println("我重写了父类的tell方法");

    }

    void say(){

    super.say();

    System.out.println("我重写了父类的say方法");

    }

    }

    public class ExtendDemo04 {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    B b = new B();

    b.tell();

    b.say();

    }}

    重写与重载的区别:

    final关键字

    1, final关键字在java中被称为完结器, 表示最终的意思

    2, final能声明类, 方法, 属性: 1)使用final声明的类不能被继承  2)使用final声明的方法不能被重写  3)使用final声明的变量变成常量, 常量是不可以被修改的, 用final修饰的变量名称一般用大写字母

    抽象类

    1, 抽象类概念:  包含一个抽象方法的类就是抽象类

    2, 抽象方法: 声明而未被实现的方法, 抽象方法必须使用abstract关键字声明

    3, 抽象类被子类继承, 子类(如果不是抽象类)必须重新抽象类中的所有抽象方法

    4, 定义格式:

    5, 抽象类不能直接实例化, 要通过其子类进行实例化

    代码:

    abstract class ABs{

    private int age;

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public abstract void tel();

    }

    class ABsDemo extends ABs{

    @Override

    public void tel() {

    System.out.println("年龄为:"+getAge());

    }

    }

    public class AbsDemo001 {

    public static void main(String[] args) {

    ABsDemo a = new ABsDemo();

    a.setAge(20);

    a.tel();

    }}

    接口:

    1, 接口是Java中最重要的概念, 接口可以理解为一种特殊的类, 里面全部是由全局常量和公共的抽象方法所组成

    2, 接口的格式: 

    3, 接口的实现也必须通过子类, 使用关键字implements, 而且接口是可以多实现的

    4, 一个子类可以同时继承抽象和实现接口

    5, 一个接口不能继承一个抽象类,但是却可以通过exends关键字同时继承多个接口,实现接口的多继承

    代码:

    //接口类

    interface inter1{

    public static final int AGER = 100;

    public abstract void tel();

    }

    interface inter2{

    public abstract void say();

    }

    //抽象类

    abstract class AbS{

    public abstract void print();

    }

    //一个接口可以通过extends关键字同时继承多个接口, 实现接口的多继承

    interface inter3 extends inter1, inter2{

    }

    // 一个子类可以同时继承抽象和实现接口

    class A extends ABs implements inter1, inter2{

    @Override

    public void tel() {

    System.out.println(AGER);

    }

    @Override

    public void say() {

    }

    public void print() {

    }

    }

    public class InterDemo01 {

    public static void main(String[] args) {

    A a = new A();

    a.tel();

    a.say();

    a.print();

    }

    }

    方法的定义

    1, 方法就是一段可重复调用的代码段

    2, 定义格式:   访问修饰符  返回值类型  方法名() { 方法主体 }

    方法重载:  方法名称相同, 但是参数的类型和个数不同, 通过传递参数的个数和类型不同来完成不同的功能

    代码:

    public static void tell(int i, int j) {

    System.out.println(i+j);

    }

    public static void tell(int i, int j, int n) {

    System.out.println(i+j+n);

    }

    public static void main(String[] args) {

    tell(10, 20);

    tell(10, 20, 3);

    }

    类的定义, 声明及使用

    1, class 类名称{ 属性   方法}

    2, 声明一个类需要通过一个关键字class

    类与对象的关系:

    1, 类是对某一类事物的描述,是抽象的,概念上的意义, 对象是实际存在的该类事物的每一个个体, 也被称为实例

    内存划分

    面向对象编程思想

    面向过程: 不去想做什么样子的盒子, 随机取工具制作

    面向对象: 先想好做一个什么样的盒子, 再去找对应的工具去做

    面向对象三大基本特征: 封装性: 对外包不可见   继承: 扩展类的功能   多态性: 方法的重载,对象的多态性

    方法的递归调用

    1, 递归调用是一种特殊的调用形式, 就是方法自己调用自己

    代码:

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    add(100);

    }

    //  1+...+100的和

    public static int  add(int num) {

    if (num == 1) {//程序出口

    return 1;

    } else {

    return num+add(num-1);

    }}

    Java关系运算

    1, Java还有提供了对两个量之间的关系进行比较的运算, 成为关系运算

    2, 关系运算的结果是true或false

    案例: 键盘输入分数, 显示该分数是否及格:

    代码: 

    public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("输入分数:");

    int score = scanner.nextInt();

    boolean isPass = score >= 60;

    if (isPass) {

    System.out.println(score + "及格" + isPass);

    } else {

    System.out.println(score + "不及格" + isPass);

    }}

    引用传递

    代码:

    class Ref1{

    int temp = 10;

    }

    public class RefDemo01 {

    public static void main(String[] args) {

    Ref1 r1 = new Ref1();

    r1.temp = 20;

    System.out.println(r1.temp);//20

    tell(r1);

    System.out.println(r1.temp);//30

    }

    public static void tell(Ref1 r2) {

    r2.temp = 30;

    }}

    this关键字

    1, 表示类中的属性和调用方法

    2,调用本类中的构造方法

    3, 表示当前对象

    代码:

    class Teacher{

    private String name;

    private int age;

    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;

    }

    //this.age 表示类中的属性和方法

    public Teacher(String name, int age){

    this();//调用本类中的构造方法, 必须放在第一行

    this.age = age;

    this.name = name;

    }

    public Teacher() {

    System.out.println("无惨构造方法");

    }

    public void tel() {

    System.out.println("姓名:"+this.getName() + "\n年龄"+ this.getAge());

    }

    }

    public class ThisDemo01 {

    public static void main(String[] args) {

    Teacher t = new Teacher("zhang", 13);

    t.tel();}}

    代码: 表示当前对象

    class Peopue{

    public void tell() {

    System.out.println(this);//com.jikexueyuan.thisdemo.Peopue@677327b6

    }

    }

    public class ThisDemo02 {

    public static void main(String[] args) {

    Peopue p = new Peopue();

    System.out.println(p);

    p.tell();//com.jikexueyuan.thisdemo.Peopue@677327b6

    }

    }

    相关文章

      网友评论

          本文标题:Java - Day5

          本文链接:https://www.haomeiwen.com/subject/uirtjttx.html