0
点赞
收藏
分享

微信扫一扫

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() );
}
}

天道酬勤



举报

相关推荐

0 条评论