#Java Day08 review + internal class + abstract class

Java Day08

review

Characteristics of facing objects

Encapsulation, inheritance, abstraction, polymorphism

How to write a class?

Class: attributes and methods abstracted from the same thing
Object: instantiate a class to an object
Class contains:
Attribute member variable, construction method, general method, member method
bean object: attribute. Generally, there are get/set methods

keyword

Access modifier:
Public, protected (current class, current package, parent-child relationship)
default (same kind, same package) and private (this class)

static keyword:

  1. Class is loaded and executed only once
  2. Priority over object execution
  3. Normal methods cannot contain static constants
  4. this super cannot be used
    5. Variables modified by static are called class variables and static variables
    6. Static modified methods are called (class methods, static methods)
    7. Variable or method class name of method static. Variable name or class name. Method name
    8. Static modified statement block (static code block)
    9. Static modified methods cannot directly call member variables and member methods, but they have to be called through objects, but ordinary methods can directly call static modified variables and static modified methods
    10. static modifies the inner class

this keyword and super keyword
this
1. Represents the current object
2. Distinguish between local variables and member variables
3. The constructor called by this () and this (parameter) is the constructor inside its own class
4.this. Property, this This method can be omitted

super
1. Object representing parent class
2.this and super cannot appear at the same time
3. Call the constructor of the parent class
4. Subclass wants to call super Attribute must have permission super method
super () is added to the first line of subclass construction method by default

final keyword
final modifies classes, constants, and methods
The decorated class cannot be inherited (the reference of the object remains unchanged, but the properties of the class object can be re assigned)
Modifier methods cannot be overridden by subclasses
Modifier variable constant public static final double PI = 3.14

Single inheritance and multiple implementation of extensions

Object is the top-level parent of all classes
A extends B
B extends Object
A actually inherits from Object

Override and reload

Rewrite: the method name is the same, the parameter list is the same, and the return type is the same. The more the access modifier is, the looser the class is
Overload: in the same class, the method name is the same, and the parameter list has different order, number and type
The construction method is overloaded
Rewrite and E: toString() and equals()
Object.toString()

==Difference between and equals
==Compare basic data types
equals compares the address of the Object
The address String in the parent class is still compared. equals has been overridden
Generally speaking, to compare the equality of two objects, you need to override equals. If equals is overridden by default, you also need to override hashcode. Hashcode is equal, and equals is not necessarily equal

Statement block

1. Outside of class / method
Static modified code block is called static code block
What is not modified by static is called construction code block and free statement block
2. The code block in the method is called ordinary code block

Static code block, free statement block, construction method

Static code block objects are created only once and executed only once
Execution order with inheritance relationship
The static code block of the parent class and the static code block of the subclass are executed only once. The free statement block of the parent class and the free statement block of the constructor subclass are executed once

Inner class

Define another class at the end of the class
If a class Lnner is defined inside the class Outer, the class Lnner is called an inner class
be careful:
1. The internal class can directly call the attributes of the external class, but the attributes declared by the internal class are not only accessed by the external class
2. The internal class can also be called from outside the external class by creating an object, as long as the internal class is declared as public
3. Internal classes can be defined not only in classes, but also in methods
4. If the internal class wants to reference the current object of the external class, you can use the external class name this to access the properties and methods of the external class

  • Classification of internal classes
  • Member inner class defines an inner class at the position of member variable
  • Method inner class defines an inner class at the method location
  • Anonymous inner classes are described in detail in the next section of this article

case

package com.ychs.demo;

/**
 * 
 * @author yangkai
 * @version 1.0
 * Outer Externally declared classes can be accessed directly by internal classes
 *       Internally declared classes cannot be accessed directly by external classes
 *       Internally declared classes can be declared through classes this. Property class accesses an external property with the same name
 */
public class Day08_02 {
	// Generally, name cannot be assigned directly
    private String name = "zs";
    // External methods cannot access properties and methods of internal classes
    public void show() {
    // System.out.println(name2);  Internal classes cannot be accessed directly
    	System.out.println("Show...");
    //You can create an object to call an external class, and create an object of an internal class to call
    	Inner inner = new Inner();
    	System.out.println(inner.name2);
    	inner.display();
    	
    }
    // Define internal classes. Internal classes can directly access the properties and methods of external classes
    public class Inner{
    	private String name2 = "lis";
    	public void display() {
    		System.out.println(name+","+name2);    		
    	}
    }
   public static void main(String[] args) {
	Day08_02 demo02 = new Day08_02();
	demo02.show();
	// Reference internal classes through external classes 
	Day08_02.Inner inner = demo02.new Inner();
	inner.display();
	
	Day08_02.Inner inner2 = new Day08_02().new Inner();
	inner2.display();
} 
}
2, An internal class can be created in an external method
public class Day08_03 {
	
    private String name01 = "yk";
    public void show() {
    	
    	//Create an inner class in an outer class method
    	class Inner{
    		private String name = "kk";
    		public void display() {
    			System.out.println(name+","+name01);
    		}
    	}
    	Inner inner = new Inner();
    	System.out.println(inner.name);
    	inner.display();
    }
    public static void main(String[] args) {
		Day08_03 demo03 = new Day08_03();
		demo03.show();
	}
}
3, The inner class of the same attribute passes the class command.this.Property references an external class
public class Day08_04 {
    private String name = "zs";
    public void show () {
    	System.out.println(name);
    }
    public class Inner {
    	private String name = "ls";
    	public void show() {
    		System.out.println("Innername   "+name);
    		System.out.println("Outnername   "+Day08_04.this.name);
    		System.out.print("Outerfunction  show() ");
    		Day08_04.this.show();
    	}
    }
    public static void main(String[] args) {
		Day08_04.Inner inner = new Day08_04().new Inner();
		inner.show();
	}
}
4, The static inner class is referenced through the class name.Internal class object name = new Quasi life.Internal class life; Object name.attribute/method
public class Day08_05 {
    public static class Inner{
    	public void show() {
    		System.out.println("show....");
    	}
    	public static void diaplay() {
			System.out.println("display....");
		}
    }
    public static void main(String[] args) {
    	Day08_05.Inner inner = new Day08_05.Inner();
		inner.show();
		Day08_05.Inner.diaplay();
	}
}

Anonymous Inner Class

Anonymous inner classes are actually anonymous subclasses or anonymous implementation classes;
This anonymous inner class integrates definition, object construction and use; And this class object is used only once

1. Because there is no name, anonymous inner classes can only be used once.
2. Anonymous inner classes are often used to simplify code writing.
3. There is a prerequisite for using anonymous inner classes. You must inherit the parent class or implement an interface.
4. Anonymous inner classes are often used for multithreading.

Use the parent class name or interface name to represent the anonymous subclass or anonymous implementation class;

1. An anonymous inner class is considered to be a subclass of a class. The declared class body is removed
2. Anonymous class is a subclass

2, Anonymous internal classes are divided into:

A. Inherited anonymous inner class.
B. Interface style anonymous inner class.
C. Parameterized anonymous inner class.

3, Precautions
In the process of using anonymous inner classes, we need to pay attention to the following points:
1. When using anonymous internal classes, we must inherit a class or implement an interface, but we can't have both. At the same time, we can only inherit a class or implement an interface.
2. Constructors cannot be defined in anonymous inner classes.
3. There cannot be any static member variables or static methods in an anonymous inner class.
4. Anonymous inner classes are local inner classes, so all restrictions on local inner classes also apply to anonymous inner classes.
5. An anonymous inner class cannot be abstract. It must implement all abstract methods of the inherited class or the implemented interface.
6. When the formal parameter of the method needs to be used in the internal class, the formal parameter must be final.

case

// The Bird class of the first package bean
package bean;

/**
 * 
 * @author yangkai
 * @version 1.0
 */
public class Bird {
    private String name;
    /**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	public int fly() {
    	return 0;
    };
}
// Override Bird class of the second package
package com.ychs.demo;

import bean.Bird;

/**
 * 
 * @author yangkai
 * @version 1.0
 */
public class Day08_10 {
	public void test(Bird b) {
		b.setName("Bird");
		System.out.println(b.getName()+" fly "+b.fly()+"M");
	
	};
    public static void main(String[] args) {
		Day08_10 b = new Day08_10();
		b.test(new Bird() {
			public int  fly() {
				System.out.println();
				return 100;
			}
			
		});
		
		
	}
}
2, Inherited inner class
/**
 * Inherited anonymous inner class cases:
 * 
 * @author shengyuee
 * @version 1.0
 */
public class TestInner {
	public static void main(String[] args) {
		Cat cat = new Cat();
		cat.speak();
        
		//Simulate cat, write an inherited anonymous inner class, override the parent class method, and the anonymous inner class is executed only once
/*		Animal cat1 = new Animal() {
			@Override
			public void speak() {
				System.out.println("Cats meow and meow);
			}
		};
		cat1.speak();*/
		
		new Animal() {
			@Override
			public void speak() {
				System.out.println("Cat meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow");
			}
		}.speak();
	}
}

class Animal {
	public void speak() {
		System.out.println("Animal call");
	}
}

class Cat extends Animal {
	@Override
	public void speak() {
		System.out.println("Cat meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow");
	}
}
3, Parametric anonymous inner class case
public abstract class Bird {  
    private String name;  
  
    public String getName() {  
        return name;  
    }  
  

abstract class

Abstract method: a method has only a declaration part and no method body
Abstract class: a class decorated with abstract. A class containing abstract methods must be an abstract class.
When the same function appears in multiple classes, but the main body of the function is different, it can be extracted upward. At this time, only the function definition is extracted, not the function subject. Use abstract to modify the method definition of this function. It is an abstract method and placed in an abstract class.

Define format:
Abstract class abstract class name{
Attributes;
Common methods;
Access control permission abstract return value type method name (parameter)// abstract method (no method body)
}
Use of abstract classes
(1) Define a subclass to inherit an abstract class
(2) Override all abstract methods in an abstract class

Characteristics of abstract classes:
1. There can be no abstract method in an abstract class, but the abstract method must be in the abstract class.
2. Abstract methods and abstract classes must be decorated with the abstract keyword
3. Abstract classes cannot create new objects because calling abstract methods is meaningless
4. To use abstract methods in abstract classes, subclasses must copy all abstract methods, create subclass objects, and then use subclass objects to call them.
If a subclass only covers part of the abstract methods, the subclass is still an abstract class.

	5,Subclasses of abstract classes
		a,It can be a concrete class that implements an abstract method
		b,Maybe it's an abstract class

matters needing attention:

(1) Can abstract classes create objects?
Abstract classes themselves cannot create objects.
(2) Does an abstract class have a constructor?
However, abstract classes have construction methods, and the parameterless construction methods in the parent class will be called by default before the instantiation of subclasses.
(3) What is the role of abstract classes?
A class that is specifically used as a parent class and is inherited
(4) Must there be abstract methods in abstract classes?
Not necessarily. No
(5) Do classes with abstract methods have to be abstract classes?
yes
(6) Can abstract methods be declared with private? Can abstract and private coexist?

		Do not use abstract methods private Declaration, because the abstract method itself is overridden by subclasses, if used private Subclasses cannot be overridden if declared. abstract And private Cannot coexist.

(7) Abstract classes cannot use final declarations?
Abstract classes cannot use final declaration because the abstract class itself is inherited. If final declaration is used, it can no longer be inherited by subclasses.

(8) Abstract class must be a parent class?
Yes, because it comes from continuous extraction.

Differences between abstract classes and ordinary classes:

            abstract class  				            General class

Define class abstract keyword No, abstract keyword

Class member Attribute, general method, abstract method Properties, general methods,

Construction method Yes, but it cannot be used You can create objects or assign initial values

Tags: Java Class abstract class

Posted by johnska7 on Fri, 20 May 2022 13:44:27 +0300