Java Basic notes (2)

Source: Internet
Author: User

Abstract class:
Abstract modified classes can have abstract methods or non-abstract methods.
An abstract class cannot instantiate an object.
If a subclass inherits an abstract class, all the abstract methods in the abstract class must be implemented.
The abstract method must be in an abstract class.
1 abstract class must be a parent class? is not necessarily a parent class, you can inherit other classes or implement interfaces
2 can an abstract class have a constructor method? There, for initialization of subclass objects
3 classes with abstract methods must be abstract classes? Must
4 There must be an abstract method in the abstract class? Not necessarily
5 abstract methods can not be used in conjunction with which keywords?
Final: The abstract class must have subclasses, the final decorated class cannot have subclasses
Static: The abstract method cannot be called, and the statically method is called by the class name
Private: The abstract method must be overridden by a quilt class, and private methods cannot be overridden

Interface interface:
Interface interface Name {
Abstract method (public abstract)
Global Constants (public static final)
}

接口:interface 规范接口可以解决单继承的问题。接口支持多继承。子类实现接口是,必须重写接口中的抽象方法。创建子类对象时,调用子类重写的方法。接口好处:    1 规范    2 提高程序可扩展性    3 降低类之间的依赖关系

The basic functions of the inheritance system can be obtained through inheritance.
Implementing an interface can get extra functionality outside of the inheritance system

Polymorphic:
Transition up: Parent class object name = new Subclass ();
Downward transformation: Subclass Object name = (subclass) parent class object;
Prerequisite: Upward is the premise of downward, need to strong turn
An exception may be thrown:. ClassCastException
Resolution: Judging
If (object instanceof class name) {}

Characteristics of members in polymorphic states:
Member variables: Which variables can be accessed at compile time look at the parent class, and the result looks like the parent class
member functions: Which methods can be accessed at compile time to see the parent class, execute the result to see the subclass (precondition subclasses override the parent class's method, not rewrite or look at the parent class)
Static member functions: Compile execution all look at the parent class

Object class: Parent class for all classes
String toString (): The class name of object @16 the binary hash code
int hashcode (): Hash code-set Collection
Boolean equals (): compare address = = If the comparison object needs to be overridden
Class GetClass (): Get the name of the classes--reflection
Basic methods of Ownership:
Clone (): Returns a copy of the object
Equals (Object obj): Determines whether two objects are equal (the default is to compare object references instead of values)
Finalize () Call this method when the GC determines that no more references to the object exist
GetClass () Returns the object run-time class
Hashcode () return hash code
Notify () wakes up a single thread waiting on the object monitor
Notifyall () wakes up all the threads waiting on the object monitor
ToString () returns the string representation of the object (by default: The class name of object @16 the binary hash code)
Wait () to have a thread waiting for
Wait (long timeout) for a certain amount of time

Template mode:
When defining a feature, part of the feature is deterministic, while the other part is undefined.
The identified parts need to be used in an indeterminate part.
Exposing an indeterminate part to a subclass to achieve

    abstract class  类名{        //确定部分        public void f(){            f1();        }        //不确定部分        public abstract void f1();    }

Instance:
public class Templetmodel {

public static void main(String[] args) {    SuperMan spiderMan = new SpiderMan();    SuperMan ironMan = new IronMan();    spiderMan.saveWorld();    ironMan.saveWorld();}

}
Abstract class Superman {
public void Saveworld () {
Save ();//First step
Overcome ();//Second step
}
Abstract public void Save ();
Abstract public void overcome ();
}
Class Spiderman extends Superman {
@Override
public void Save () {
System.out.println ("I'm Spider-Man, I'm saving the World");
}
@Override
public void Overcome () {
System.out.println ("I am Spider-Man, I have saved the world");
}
}
Class Ironman extends Superman {
@Override
public void Save () {
System.out.println ("I am Iron Man, I am saving the World");
}
@Override
public void Overcome () {
System.out.println ("I am Iron Man, I have saved the world");
}
}
Call: Ironman Spiderman = new Spiderman ();
Spiderman.saveworld ();

Inner class:
member Inner class: The inner class is in the position of the outer class member, so the inner class can directly
accessing members of an external class
Static inner class: Builder usage (builder)
Local inner class
Anonymous inner class

特点: 内部类可以直接访问外部类成员        外部类要访问内部类的成员,必须建立内部类的对象作用:    1 隐藏部分信息    2 可以访问外部类的私有成员    3 弥补了单继承的局限性    4 解决问题:如果要描述一个事物中还包括另一个事物,        同时这个事物要访问被描述的事物生成字节码文件:外部类$内部类.class一  成员内部类:内部类处在了外部类成员的位置上,所以内部类可以直接    访问外部类的成员[public/default]class 外部类{    属性    方法    内部类:    [访问修饰符] class 内部类{ //public private default protected        属性        方法    }}说明:1 内部类可以直接调用外部类的成员,包括private2 如果内部类与外部类的属性或者方法同名时,则内部类默认调用内部类自己的。    如果想调用外部类的        外部类.this.成员3 创建内部类对象    1)        外部类 外部类对象 = new 外部类();        外部类.内部类 内部类对象 = 外部类对象.new 内部类();    2)        外部类.内部类 内部类对象 = new 外部类().new 内部类();4 如果外部类想调用内部类成员,需要通过创建内部类对象来调用。5 编译之后生成的内部类字节码文件为:外部类$内部类.class

Instance:

Class Outer1
{
private int num = 5;
Class Inner1//member Inner classes
{
int num = 500;
public void Show () {
System.out.println ("inner–>num=" +num);
System.out.println ("outer–>num=" +outer1.this.num);
}
}

public void test(){  //外部类的方法    Inner1 in = new Inner1();//外部类访问内部类成员,需要通过对象访问    in.show();}

}

Two static inner classes: equivalent to an external class, because the static modifier is already loaded into memory when the initial barbarian class is initialized
[Public|default] class outer class {
Property
Method
Static Inner class
[Access modifier] static class inner class {
Property
Method
}
}
Attention:
1 cannot invoke external class non-static members
2 Allow non-static variables and methods to be defined
3 An external class cannot be used in an inner class. This
4 format used by static inner classes (not dependent on external class objects)
External class. Inner class Inner Class object = new External class. Inner class ();
Instance:
Class Outer2
{
static int num = 10;
Static class Inner2//statically internal classes
{
public static void Show () {
System.out.println ("num=" +num);
}
}
public void Test () {
Inner2.show ();
Inner2 in = new Inner2 ();
In.show ();
}
}

Three local inner classes: defined inside a method
[Public|default] class outer class {
Property
Method () {
Class Inner Classes {

        }    }}应用:多线程、网络下载注意:1 局部内部类或者局部方法的访问修饰符只能是default的2 局部内部类可以访问外部类成员3 局部内部类如果想使用所在方法的局部变量时,该变量必须是final的(因为final存在于方法区,生命周期较长,不      影响局部内部类的使用)4 局部内部类定义在方法中,适用范围仅在方法内

Instance:
Class Outer3
{
int a = 5;
void Test () {
final int b = 100;
Class Inner3
{
public void Show () {
SYSTEM.OUT.PRINTLN ("External class member a=" +a);
System.out.println ("member of the Method b=" +b);
}
}//end class
Inner3 in = new Inner3 ();
In.show ();
}//end Test ()
}

Anonymous inner class: shorthand for inner class, no class name
Anonymous inner classes are defined only when the object is used once

Premise: An anonymous inner class must inherit or implement an external class or interface

Application: Apply to abstract classes and interfaces to increase program flexibility

Grammar:
Class Name Object name = new class name () {//abstract class or interface name
Implement the method of an abstract class or interface
};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basic notes (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.