java复习笔记1

    java中类的继承

  java中类的继承

运行下面代码

复制代码
class Person{
    private String name = "";
    public String getName() {
        return this.name;
    }
    public void setName(String n) {
        this.name = n;
    }
}

class Student extends Person{
    
}

public class ExtendDemo {
    public static void main(String arg[]) {
        Student stu = new Student();
        stu.setName("nono");
        System.out.println(stu.getName());
    }
}
复制代码

 

  多层继承

  java的多层继承

运行下面代码

复制代码
class A {
    public String name = "nn";
}
class B extends A{
    public int age = 1;
} 
class C extends B{
    public void tell() {
        System.out.println(this.name);
        System.out.println(this.age);
    }
} 

public class ExtendsDemo1 extends B{
    public static void main(String args[]) {
        C c = new C();
        c.tell();
    }
}
复制代码

   

  super

  java中调用超类, 和超类的方法:

运行下面代码

复制代码
class Person{
    public Person () {
        System.out.println("Person Constructor");
    }
    public void tell() {
        System.out.println("Person tell");
    }
}
class Student extends Person {
    public Student() {
        super();
        System.out.println("Student Constructor");
    }
    public void tell() {
        super.tell();
        System.out.println("Student tell");
    }
}

public class extendsDemo2 {
    public static void main( String args[] ) {
        Student stu = new Student();
        stu.tell();
    }
}
复制代码

 

  重载  .  复写  .  this

  java中的重载(overloading)是:在同一个类中,对权限没有要求, 方法的参数不同;

  java中的复写(overriding)是:不是同一个类中, 对权限要求(default, public), 方法的参数, 返回值, 名称都必须一模一样;

  java中的this一直是指向实例的; 要调用父级的构造函数, this()必须放在第一行, super是一只指向父级的;

 

   final

   final声明的类, 不能被继承;

   final的方法, 不能被子类复写;

   final变量不能被修改;

运行下面代码

复制代码
public class FinalDemo {
    public static final String INFO = "hehe";
    public static void main (String args[]) {
        System.out.println(INFO);
    }
}
复制代码

 

 

  interface

  定义接口interface, 让别的类去实现;

运行下面代码

复制代码
interface A {
    public String name = "aaaa";
}
interface B{
    public void getName();
}
public class InterFaceDemo implements A,B{
    public void getName() {
        System.out.println(this.name);
    }
    public static void main (String args[]) {
        InterFaceDemo in = new InterFaceDemo();
        in.getName();
    }
}
复制代码

 

   abstract

  abstract的方法必须有abstract的声明, 子类继承必须通过extend继承abstract类:

运行下面代码

复制代码
interface A {
    public String a = "a";
}
abstract class B {
    public abstract void say();
}
public class AbstractDemo extends B implements A{
    public String a = "heheda";
    public void say() {
        System.out.println(this.a);
    }
    public static void main( String args[] ) {
        AbstractDemo abs = new AbstractDemo();
        abs.say();
    }
}
复制代码

 

  java转型

   java转型, 向上转型和向下转型, 向上转型是自动的, 向下转型是强制的

运行下面代码

复制代码
class A {
    public void fun1() {
        System.out.println("A print fun1");
    }
    public void fun2() {
        this.fun1();
    }
}

class B extends A{
    public void fun1() {
        System.out.println("B print fun1");
    }
}

public class PolDemo {
    public static void main(String args[]) {
        B b = new B();
        A a = (A)b;
        a.fun1();
        a.fun2();
    }
}
复制代码

 

 

  instanceof的使用

运行下面代码

复制代码
class A {

}
class B extends A {

}
public class InstanceOfDemo {
    public static void print(String s) {
        System.out.println(s);
    }
    public static void print(Boolean b) {
        System.out.println(b);
    }
    public static void main (String args[]) {
        B b = new B();
        print("b instanceof B");
        print(b instanceof B);
        print("b instanceof A");
        print(b instanceof A);
    }
}
复制代码

 

 

  Object是所有类的父类

  所有方法的参数都可以用Object作为参数类型接收

运行下面代码

public void print ( Object o ) {

}

 

  

  java的包装类

运行下面代码

复制代码
public class WrapClass {
    public static void main(String args[]) {
        Integer in = new Integer(1);
        System.out.println( in.intValue() );
        Character ch = new Character('a');
        System.out.println( ch.charValue() );
        Short sh = new Short("1");
        System.out.println( sh.shortValue() );
        Long lon = new Long(2222);
        Float flo = new Float(22);
        Double dou = new Double(2222);
        Boolean b = new Boolean("true");
        Byte by = new Byte("10");
        System.out.println( by.byteValue() );
    }
}
复制代码

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/5220131.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(385)  评论(0编辑  收藏  举报
历史上的今天:
2014-03-03 最近新学的小东西和单词

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示