My java learning log day10

java learning log day10

July 10, 2019

Past

final (final)

关键字---修饰值--数据、方法、类

Data - can not change the value of
class A {

stiatic final i;

The initial value of a static area, just mark the initial value, there is no actual meaning, a new version of java to make up for this defect
}

When the final modification methods, the method can be overloaded, can not be overridden

- - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
About abstract methods abstract;

If there is a method of abstract class, the class must also be modified with abstact, an abstract class into the class

Format public abstract type name ();

public abstract double xx();

Abstract method has no method body, but provides a way for subclasses override;

There are methods such as the method body public abstract double xx () {return 0.0;}

The method is called a common method or entity;

If an ordinary class inherits an abstract class, abstract method had to be overridden by subclasses, or an error;

If you do not want to override all the abstract methods, you can subclass with abstract modified into an abstract class;

All subclasses of a parent class party have carried out varying degrees of rewriting, this method of parent class

Method body no practical meaning, and the method thereof can be removed by abstract modification becomes abstract methods.

Abstract methods can be overloaded

Abstract methods can not be static / final / private were modified because they were not override this modified

An abstract class is not necessarily contains abstract methods, abstract methods must be abstract class;

Abstract class contains constructors, other programming languages ​​to create an object via the constructor, but the object for

For java can not obtain, so the java angle can be understood as, can not create objects in java abstract class

The final class final class can not be inherited

-–*------------------**--------*–**-

interface- interfaces, not classes;

If all the methods in the class is an abstract method of the abstract, it may be converted into an interface class

E.g

interface Xx{

public abstract double x();

public abstract double y();

}

How to classes and interfaces associate relations

By keyword implement - let classes and interfaces associate relations

And a common class implements an interface, it is necessary to rewrite all abstract methods;

class Yy implements Xx{

}

If you do not want to override all the abstract methods, the class will become an abstract class;

E.g

abstract class Yy implements Xx{

}

A class can implement multiple interfaces

例如
class Yy implements Xx,Zz{

}

Interface can be inherited interface, the interface with the interface is the same, and can be multiple inheritance

E.g

interfaces Xx extends Yy,Zz{

}

Precautions:

Interface can not create object; constructor because it is not defined;

Within the interface can be given property and assignment, but can not be declared, property values ​​are

public static final modification of default;

Abstract method in java interface default is public abstract modified

E.g

interface Xx{

 double x();

 double y();

}

Downcast

After about downcast, as the name suggests, to create the parent class declaration subclass object and then declare the original object subclass

That is, if the parent class declaration, the parent class is called, if we want to use subclasses are not rewritten Other

When the methods and properties, downcast;

When downcast, you must first downcast to shape up

For example,
two classes inherits
class A {}
class B {A} the extends

In the main method
modeling upwardly
A b = new B ();

Then downcast;
B = D (B) B;

About interface

Is a tree structure between classes, can quickly detect a relationship between two classes, have been detected between compile and run

Is between classes and interfaces of the reticular formation to achieve, can not quickly examine the relationship between the two types, it is not detected at compile time,

Achieved only if there is a relationship between the type of detection operation period.

Interface cast acceptable values ​​of all types (reference type)

How will the entity method defined in the interface:

From the start java1.8, java entity provides two methods defined in an interface method;

Cc action such // computer
interface cc {

// sum
// entity method was modified default - default entity method

public default int sum(int m,int n){

  return m+n;

}

The second method is static entities defined static method

public static int jt(int m,int n){

  return m+n;

}

}

interface ca{

int max(int m,int n);

}

From jdk1.8 provide a way to start rewriting step method of interface

Use Lambda Expressions

Lambda expressions are using the premise, interface must be only an abstract method;

ca c =(int m,int n)-> {return m>n?m:n;}

Format (parameter list) -> {} method body overridden method

If the method is only one body of code may be omitted and a return {}

E.g

ca c =(int m,int n)-> m>n?m:n;

Or ca c = (m, n) -> m> n m:? N;

If the interface is only an abstract method, this interface is a function interface.

// Sort the array to

interface are{

Abstract method

void sort(int[]arr);

}

In the main method

Create a new array
int [] arr = {1,8,6,5,0,2,3} ;

Lambda expressions using the overwriting
are a = (int [] arr1 ) -> {Arrays.sort (arr1);};

Call interface method
a.sort (arr);

Output array
System.out.println (Arrays.toString (arr));

If the case of a parameter, the parameter can be removed parentheses;

Simplest form as are a = Arrays :: sort but not commonly used

Two colons mean is, if you have been using a static method is a double-colon;

After the double colon must guarantee a static method;

Interface advantages - advantages

1、当做一个约束或者当做一个模板;

2、向上造型类型统一(接口类型);

----------**----------------------------------**–-*

About inner class

Class or interface name suggests is further defined a class

Then define a class class

方法内部类

成员内部类

静态内部类 

匿名内部类

Method inner class

Method inner class defines the properties and methods of non-static and static const

Can inherit and implement, access modifier can not be modified, can be final

Or abstract modification;

Internal class can obtain all of the information outside the class, including private information modification;

You can get information in the method, but only if the information in the method is a constant

// external class

class A{

intj = 1;

//method

public void m(){

// inner class method

Methods inner class can inherit class, you can also implement interfaces

class Int extends XX implements XY {

static final int i =10;

public void mn(){}

}
Int a = new Int();

}

public void n(){}

}

Members of the inner class

Create a member of the inner class object method in the main method

XX.XY a =new XX().new XX2();

class XX{

Create objects within the class members;

new XX().new XX2();

XX.XY a =new XX().new XX2();

int x =1;

// Create a member of the inner class

You can define non-static properties and methods can also be realized extends inheritance and implemment

Access modifier can be modified, and the final class can be final and abstract abstract

Normal and the same class, all the properties and methods may also be the outer class get;

class XX2{

int i = 1;

public void n(){}

}

}

Static inner classes

class XX{

int i =1;

Internal static class may define any information

Can inherit and implement extends realization

Access modifier can be treated as final and the final class and abstract abstract

You can only get static information to the outer class
static class XY {

int k =10;

public void n(){}

}

}

Static inner classes to create objects in the main method

XX.XY x = new XX().XY();

Anonymous inner classes

Create an anonymous inner class

Anonymous inner class is an abstract class inheritance, override method

A a = new A(){

}

Anonymous inner classes can also inherit common class and override the method

B b = new B(){

}

Anonymous inner classes can also implement interfaces

D d = new D(){

}

Abstract
abstract class A {

Methods
public abstract void m ();

}
General category
class B {}

Interface
interface D {};

If the anonymous inner class appears in the process, it was seen as a way to inner class;

If the anonymous inner class members appear in position, press members to use inner classes

A class defined in the interface

Internal default class interface is static;

interface A{

static class B{

int i =10;
}
}

About the internal interface

E.g

interface A{

static class B{

int i =10;
}

interface D{

100 J = int;
}
}
interface to define an interface, the interface in the internal class can also define the interface

All internal interfaces are static by default;

Guess you like

Origin blog.csdn.net/qq_31783363/article/details/95365765