java17:修饰词

修饰词:

    限定属性/方法/类的访问范围

private 只在类内部可见

publice 任何地方都可见

protected 保护


protected 受保护的
private私有的
private 只能在本类中调用;
protected表示该属性或方法可被同一包内或子类使用.

什么都不添加的 默认的


package 包 也是范围

    跨包 访问



修饰符本类同包类子类其他类
默认可以可以不可以不可以
publice可以可以可以可以
protected可以可以可以不可以
private可以不可以不可以不可以


不知道范围: 就先 私有

很少使用默认作为修饰词


尽可能私有 ==》 尽可能的封装


类的修饰可以使用publice 和默认

publice 修饰的类可以被任何类使用

默认访问控制的类只可以被同一个包中的类使用

protected 修饰内部

private  修饰内部


私有方法不能被继承,不能被重写  被继承的方法才能重写

package day17;

public class Demo03 {

	public static void main(String[] args) {
		animal dog = new dog();
		dog.test();// 根据对象的类型 子类的dog
		// 调用 dog中的print
		// 又因为dog 的println是私用方法
		// java 自动的调用最近的方法
		// 就又调用了 animal中的 println

	}
}

class animal {
	public void test() {
		this.print(); // dog 的类型是 animal 他的对象是dog类型
		this.println();// dog 的类型传递给了this 同样的指向了对象dog类型
	}

	public void print() {
		System.out.println("a");
	}

	private void println() {// 私用方法不能重写
		System.out.println("b");
	}
}

class dog extends animal {
	public void print() {
		System.out.println("A");
	}

	public void println() {
		System.out.println("B");
	}

}


static  修饰成员变量

    静态变量

        静态变量是属于类的变量, 使用类名访问 静态变量只有一份

 

package day05;
public class D {
	public static void main(String[] args) {
		Dog  wangcai = new Dog("wangcai");
		Dog xiaowangcai = new Dog("xiaowangcai");
		System.out.println(Dog.numOfdogs);
	}
}
class Dog{
	String name;//实例变量
	static int numOfdogs;//静态变量
	public Dog(String name){
		this.name = name;
		Dog.numOfdogs++;
	}
}

    

    静态方法

        静态方法没有隐含参数 this 没有当前对象  他属于类的方法,他用类名调用

        静态方法经常用于与当前对象无关的采用静态方法

        

        非静态方法有隐含参数 this  在那个对象上调用 this 就是那个对象



static块为属于类的代码块,在类加载期间执行的代码块,只执行一次,可以用来在软件中加载静态资源(图像、音频等等)

package day05;
public class Demo04 {
	public static void main(String[] args) {	
		Ooo o1 = new Ooo();
		Ooo o2 = new Ooo();
	}
}

class Ooo{
	//System.out.println("Hi");不能直接写
	{System.out.println("HI");}//代码块,很少很少使用,在对象创建期间执行,类似于构造器重的代码
	static {System.out.println("hi");} //static 代码块 ,在类加载期间执行用于添加静态资源
}
package day05;

public class Demo03 {

	public static void main(String[] args) {
		Point p1 = new Point(3, 6);
		Point p2 = new Point(6, 9);
		System.out.println(p1.distance(p2));
		System.out.println(Point.distance(p1, p2));
	}

}

class Point {

	int x;
	int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public double distance(Point other) {
		int a = this.x - other.x;
		int b = this.y - other.y;
		return Math.sqrt(a * a + b * b);
	}

	public static double distance(Point p1, Point p2) {
		int a = p1.x - p2.x;
		int b = p1.y - p2.y;
		return Math.sqrt(a * a + b * b);
	}

}


内部类:

就是在类的内部定义的类

    内部类用于 封装 类的实现,避免暴露细节,限制类的访问范围。

package xyz.rhel.els;

import java.util.Random;

/**
 * Tetromino 这个类是抽象的 所以不能new
 * so 里面的randomOne 这个方法就要用static  static 可以是使用 类名来调用方法  简单工厂方法
 * randomOne 使用了static 所以下面的7个子类也要使用static
 * Tetromino 的构造器是私有的所以就没有子类可以来继承
 */
public abstract class Tetromino {
	private Tetromino(){}
	protected Cell[] cells = new Cell[4];
	public static Tetromino randomOne(){
		Random r = new Random();
		int type = r.nextInt(7);
		switch(type){
		case 0:
			return new T();
		case 1:
			return new I();
		case 2:
			return new S();
		case 3:
			return new Z();
		case 4:
			return new J();
		case 5:
			return new L();
		case 6:
			return new O();
		}
		return null;
	}
	
	private static class T extends Tetromino{
		public T(){
			cells[0] = new Cell(0, 4, Tetris.T);
			cells[1] = new Cell(0, 3, Tetris.T);
			cells[2] = new Cell(0, 5, Tetris.T);
			cells[3] = new Cell(1, 4, Tetris.T);
			
		}
	}
	private static class I extends Tetromino{
		public I(){
			cells[0] = new Cell(0, 4, Tetris.I);
			cells[1] = new Cell(0, 3, Tetris.I);
			cells[2] = new Cell(0, 5, Tetris.I);
			cells[3] = new Cell(0, 6, Tetris.I);
		}
	}
	private static class S extends Tetromino{
		public S(){
			cells[0] = new Cell(0, 4, Tetris.S);
			cells[1] = new Cell(0, 5, Tetris.S);
			cells[2] = new Cell(1, 3, Tetris.S);
			cells[3] = new Cell(1, 4, Tetris.S);
		}
	}
	private static class Z extends Tetromino{
		public Z(){
			cells[0] = new Cell(0, 4, Tetris.Z);
			cells[1] = new Cell(0, 3, Tetris.Z);
			cells[2] = new Cell(1, 4, Tetris.Z);
			cells[3] = new Cell(1, 5, Tetris.Z);
		}
	}
	private static class L extends Tetromino{
		public L(){
			cells[0] = new Cell(0, 4, Tetris.L);
			cells[1] = new Cell(0, 3, Tetris.L);
			cells[2] = new Cell(0, 5, Tetris.L);
			cells[3] = new Cell(1, 3, Tetris.L);
		}
	}
	private static class J extends Tetromino{
		public J(){
			cells[0] = new Cell(0, 4, Tetris.J);
			cells[1] = new Cell(0, 3, Tetris.J);
			cells[2] = new Cell(0, 5, Tetris.J);
			cells[3] = new Cell(1, 5, Tetris.J);
		}
	}
	private static class O extends Tetromino{
		public O(){
			cells[0] = new Cell(0, 4, Tetris.O);
			cells[1] = new Cell(0, 5, Tetris.O);
			cells[2] = new Cell(1, 4, Tetris.O);
			cells[3] = new Cell(1, 5, Tetris.O);
		}
	}
}


类的访问范围

 公有类:任何位置可见

 同胞类:当前包内可见

 内部类:一般用于类内部可见 如果修饰词使用public 在类的外面也是可见的

 局部内部类:匿名类 只在方法内部可见


匿名类的语法很简洁,经常使用。

package diyitian;
public class HelloJava {
	public static void main(String[] args) {	
		Ooo o1 = new Ooo();
		Ooo o2 = new Ooo();
		Ioo too = new Ioo(){};//这个是创建的一个匿名子类 他创建的是一个子类 抽象类不能直接创建 
		
	}
}

class Ooo{
	//System.out.println("Hi");不能直接写
	{System.out.println("HI");}//代码块,很少很少使用,在对象创建期间执行,类似于构造器重的代码
	static {System.out.println("hi");} //static 代码块 ,在类加载期间执行用于添加静态资源
}

 
abstract class Ioo{//抽象类
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值