实例初始化、匿名内部类
程序员文章站
2022-03-10 10:50:55
package com.atguigu.init.demo01;/** * 代码块: * 构造代码块 * 静态代码块 * 同步代码块(多线程线程安全时讲解) * * 构造代码块格式: * 位置:类中,方法外 * { * * } * * 作用:将多个构造器里面相同的代码进行向上抽取,将相同的代码存放在构造代码块中 * * 构造代码块执行顺序优先于构造器,每创建一个对象,就执行一个构造代码块 * * */public class I...
package com.atguigu.init.demo01;
/**
* 代码块:
* 构造代码块
* 静态代码块
* 同步代码块(多线程线程安全时讲解)
*
* 构造代码块格式:
* 位置:类中,方法外
* {
*
* }
*
* 作用:将多个构造器里面相同的代码进行向上抽取,将相同的代码存放在构造代码块中
*
* 构造代码块执行顺序优先于构造器,每创建一个对象,就执行一个构造代码块
*
*
*/
public class InitDemo01 {
public static void main(String[] args) {
new Student();
new Student();
}
}
package com.atguigu.init.demo01;
public class Student {
private int id;
private String name;
private int age;
private static int num = 729001;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
//this.id = num++;
this.name = name;
this.age = age;
System.out.println("有参构造器");
}
public Student() {
//this.id = num++;
System.out.println("无参构造器");
}
{
this.id = num++;
System.out.println("构造代码块");
}
}
demo02
package com.atguigu.init.demo02;
/**
* 实例初始化的步骤:
* 1.父类的隐式初始化和子类的隐式初始化
* 2.super()或者this()
* 3.实例变量显式初始化和构造代码块优先级是一样,谁在前,先执行谁
* 4.构造器里面的代码
*/
public class InitDemo02 {
public static void main(String[] args) {
new SubClass();
//new SubClass();
}
}
package com.atguigu.init.demo02;
public class SubClass {
//实例变量
int num = method();
//
//SubClass sc = new SubClass();
private int method() {
System.out.println("实例变量显式初始化");
return 0;
}
//构造器
public SubClass () {
//this(1);
System.out.println("子类的无参构造器");
}
public SubClass (int a) {
this(1,2);
System.out.println("子类的有参构造器(1个)");
}
public SubClass (int a,int b) {
System.out.println("子类的有参构造器(2个)");
}
{
System.out.println("子类的构造代码块");
}
}
demo3
package com.atguigu.init.demo03;
public class SubClass extends SuperClass{
//实例变量
int num = method();
//
//SubClass sc = new SubClass();
private int method() {
System.out.println("子类的实例变量显式初始化");
return 0;
}
//构造器
public SubClass () {
//this(1);
//super();
System.out.println("子类的无参构造器");
}
{
System.out.println("子类的构造代码块");
}
}
package com.atguigu.init.demo03;
public class SuperClass {
//实例变量
int num = method();
//
//SubClass sc = new SubClass();
private int method() {
System.out.println("父类的实例变量显式初始化");
return 0;
}
//构造器
public SuperClass () {
//
System.out.println("父类的无参构造器");
}
{
System.out.println("父类的构造代码块");
}
}
package com.atguigu.init.demo03;
public class InitDemo03 {
public static void main(String[] args) {
new SubClass();
}
}
demo4
package com.atguigu.init.demo04;
public class InitDemo04 {
public static void main(String[] args) {
SubClass sc = new SubClass();
sc.print();
}
}
package com.atguigu.init.demo04;
public class SubClass extends SuperClass{
int num = 10;
public SubClass () {
//super();
num = 20;
}
//@Override
public void print() {
System.out.println(num);
}
}
package com.atguigu.init.demo04;
public abstract class SuperClass {
int num = 1;
public SuperClass () {
//
//System.out.println(this);
this.print();
}
public abstract void print();
}
demo5
package com.atguigu.init.demo05;
/**
* 静态代码块:
* static {
*
* }
*
* 静态:随着类的加载而加载,只加载唯一的一次
*
* 静态代码块优先于构造代码块
*
*/
public class InitDemo05 {
public static void main(String[] args) {
new SubClass();
//new SubClass();
}
}
package com.atguigu.init.demo05;
public class SubClass {
int a = method();
private int method() {
System.out.println("子类实例变量的显式初始化");
return 0;
}
private static int show() {
System.out.println("子类静态变量的显式初始化");
return 0;
}
public SubClass(){
System.out.println("子类的无参构造器");
}
{
System.out.println("子类的构造代码块");
}
static {
System.out.println("子类的静态代码块");
}
static int b = show();
}
demo6
package com.atguigu.init.demo06;
public class InitDemo06 {
public static void main(String[] args) {
new SubClass();
}
}
package com.atguigu.init.demo06;
public class SubClass extends SuperClass{
int a = method();
static int b = show();
static SubClass sc = new SubClass();
private int method() {
System.out.println("子类实例变量的显式初始化");
return 10;
}
private static int show() {
System.out.println("子类静态变量的显式初始化");
return 0;
}
public SubClass(){
// 隐式初始化
// super();
// 实例变量的显式初始化和构造代码块
// 构造器里面的代码
System.out.println("子类的无参构造器");
}
{
System.out.println("子类的构造代码块");
}
static {
System.out.println("子类的静态代码块");
}
}
package com.atguigu.init.demo06;
public class SuperClass {
int a = method();
static int b = show();
//static SuperClass sc = new SuperClass();
private int method() {
System.out.println("父类实例变量的显式初始化");
return 10;
}
private static int show() {
System.out.println("父类静态变量的显式初始化");
return 0;
}
public SuperClass(){
// 隐式初始化
// super();
// 实例变量的显式初始化和构造代码块
// 构造器里面的代码
System.out.println("父类的无参构造器");
}
{
System.out.println("父类的构造代码块");
}
static {
System.out.println("父类的静态代码块");
}
}
匿名内部类
package com.atguigu.innerclass.demo05;
public interface A {
void print(String str);
}
package com.atguigu.innerclass.demo05;
public class AImpl implements A{
@Override
public void print(String str) {
System.out.println(str);
}
}
package com.atguigu.innerclass.demo05;
public class UtilClass {
public void method (A a,String str) {
a.print(str);
}
}
package com.atguigu.innerclass.demo05;
public class InnerClassDemo05 {
public static void main(String[] args) {
UtilClass uc = new UtilClass();
// 第一种方法:直接用多态思想
// 创建实现类对象
//A a = new AImpl();
//uc.method(a, "HelloWorld");
// 第二种方法:用匿名内部类进行简化
uc.method(new A(){
@Override
public void print(String str) {
System.out.println(str);
}
}, "HelloWorld");
}
}
本文地址:https://blog.csdn.net/qq_40962557/article/details/107678017
上一篇: Java单例模式的深入了解