7.29 to 8.4 weekly summary

This week's progress is quite slow, simple to learn and practice a Java polymorphism, interfaces.

Polymorphic Code follows


Polymorphic package; public class Animal { public void Show () { System.out.println ( "I'm an animal!"); } } package polymorphism; public class CAT the extends Animal { public void Show () { the System.out .println ( "I'm a cat!"); } } Package Penalty for polymorphism; public class dog the extends Animal { public void Show () { System.out.println ( "I am a dog!"); } } Package Penalty for polymorphic; public class text { public static void main (String [] args) { // do not use polymorphic Dog Dog DOG1 new new = (); dog1.show (); CAT CAT CAT1 new new = (); cat1.show ( ); // polymorphic animal animal1 = new dog (); animal1.show (); Animal animal2 new new CAT = (); animal2.show (); Animal Animal animal3 new new = (); animal3.show (); / * upcast object type subclass object -> parent class object security parent object -> subclass object unsafe * / // downcast security Dog dog2 = (Dog) animal1; dog2.show (); // downcast unsafe CAT CAT2 = (CAT) animal1; CAT2. Show (); // upcast safety Animal animal4 = CAT1; animal4.show (); } }

 There are at risk of transformation, the results are as follows:

Error for the next transition to find errors in the course of implementation, at the time of writing not found error.

Interface classes

Polymorphic package; 

public interface animal1 { 
	public void show1 (); // only the parent class interface method, implemented method in a subclass 

} 

package polymorphism; 

public class CAT1 the implements animal1 { 
	public void show1 () { 
		System.out.println ( "I'm a cat!"); 
	} 

} 

Package Penalty for polymorphism; 

public class DOG1 the implements animal1 { 
	public void show1 () { 
		System.out.println ( "I'm a dog!"); 
	} 

} 

Package Penalty for polymorphic ; 

public class text1 { 
	public static void main (String [] args) { 
       // upcast specific references to the parent class implementation dog 
		animal1 animal2 = new new DOG1 (); 
		animal2.show1 (); 
		// downcast 
	    dog1 dog2 = (DOG1) animal2; 
	    dog2.show1 (); 
		} 
}

 Results are as follows:

For an interface, the class declaration in the parent, be embodied in a subclass.

 Second:

Anonymous class

package anonymous inner classes; 

public interface A { 
   public void A (); 
} 


package anonymous inner classes; 

public class A {B the implements 

	@Override 
	public void A () { 
		// the TODO Auto-Generated Stub Method 
		System.out.println ( " used only once method "); 
	} 
	

} 

Package anonymous inner classes; 

public class text { 
	public void the Test (a a) { 
	AA (); 
	} 

 
 public static void main (String [] args) { 
	text T = new new text () ; 
	t.test (new new B ()); 
	
	// anonymous inner classes 
	t.test (new new A () { 

		@Override 
		public void A () { 
			// the TODO Auto-Generated Method Stub 
		// embodied portion	  
			System.out. println ( "hi !!");
		}});
		
	}
}
 

 Screenshot results:

 

Java study notes:


9:58:09 My phone 2019/8/4

syso Ctrl + / output statements
main ctrl + / main function of
multi-line comments shortcut key 
ctrl + shift + /
input Scanner scaner in the new new Scanner class = (System · in);
int the n- scanner.nextInt = ();
Ctrl + Shift + O println wrap package is automatically turned
outer similar goto label inside
forreach iterate
for (j; arr (array name))
the System OUT · · println (J);
dynamic memory distribution
int arr [] = new int [ 3]
static initialization int arr [] = int [] {1,2,3}
is not a fixed parameter ????? hobbies String
Ctrl + D delete a row
call the static method class name methods object. the method of
the basic structure of the internal damage category class, the benefits of ease of use property class external
execution order of the static building blocks (executed only once) universal building block constructor
String == comparison is a reference
name1.equals (name 2) compares the contents of
the string concatenation string name = "Zhang" name + = "three"
Get set shortcuts to generate the Shift + S + alt
the extends inherit
the abstract class abstract subclasses implement method of the parent class method declared
final termination constant
static static
Java is a single inheritance but can have multiple interfaces to inherit the interface after
interface can be multiple inheritance 

 

Guess you like

Origin www.cnblogs.com/cxy0210/p/11297316.html