3.java类特征(Josephus约瑟夫问题)

3.1 类方法封装

1.类变量是该类的所有对象共享的对象,一改全改了

2.定义语法:访问修饰符 static数据类型变量名

3.static

/*
 * 该例子用于练习static的用法。
 */
package com.blog;
public class StaticPractise 
{
	static int i=1;
	static
	{
       System.out.println("执行一次");
       i++;
	}//会自动执行一次,也只有一次
	public StaticPractise()
	{
       System.out.println("Hello");
       i++;
	}
	public static void main(String args[])
	{
		new StaticPractise();
	    System.out.println(StaticPractise.i);
	    new StaticPractise();
	    System.out.println(i);
	}
}


 

4.类方法(静态方法、静态函数):属于所有对象实例的

5.Java:类变量(static)原则上用类方法(static)去访问;类方法中不能访问非静态变量

,就是非类变量,但是普通的成员方法可以访问静态变量(类变量)

使用:类名.类方法名    对象名.类方法名

6.非类变量就是实例变量,属于每个对象自己的

7.Java面向对象编程的三(四)大特征:封装、继承、多态(、抽象)

8.抽象:把一类事物的共有的属性和行为提取出来,形成一个物理模板,此研究问题的方法就是抽象

9.封装:把抽象出来的数据和对数据的操作封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作(成员方法),才能对数据进行操作。

10.封装的访问控制修饰符

11.四种访问控制符:

公开级别:public

受保护级别:对子类和同一个包中的类公开

默认级别:没有修饰符,向同一个包的类公开,子类不可以访问类中默认级别的元素的

私有级别:用private修饰,只有类本身才能访问

公开>受保护>默认>私有

12表格说明四个的访问权限

访问级别

访问控制修饰符

同类

同包

子类

不同包

公开

public

受保护

protected

×

默认

 

×

×

私有

private

×

×

×

 

13.包的三大作用:1.区分相同名字的类 2.当类很多的时候可以很好的管理 3.类控制访问范围

14.包的打包命令:package  com.xiaoming 一般放在文件的开始的地方

15.引包命令:improt包名


 

3.2访问修饰符 重载 覆盖

1.一个文家中如果只有public类,那么这个public类可以不用主函数

2. 不想被继承就设为protected

3.子类最多只能继承一个父类,Java里面不允许一个子类继承多个父类,C++却可以,如Java中一定要呢,就用接口吧

4.Java的所有类都是Object的子类,继承层次数没有限制

5.JDK6.0里面有202个包,3777个类、接口、异常、枚举、注释和错误

6.在做开发的时候多使用JDK帮助文档

7.方法重载:类的同一种功能的多种实现方式,取决于调用者传递的参数

8.方法重载注意事项:

  方法名相同

  方法的参数类型,个数,顺序至少有一项不同

  方法的修饰符可以不同

  方法的返回类型可以不同

另外:

  只是返回类型不一样并不构成重载

  只是控制访问修饰符不一样不构成重载

 

3.3约瑟夫问题(丢手帕问题)

1.方法覆盖的:子类有一个方法和父类的某个方法名称、返回类型、参数一样

2.方法覆盖注意:

  子类的方法的返回类型、参数、方法名称要和父类的一样,否则编译出错

  子类方法不能缩小父类方法的访问权限,但是扩大是可以的

3.丢手帕问题:

/**
 * 该程序为经典的约瑟夫问题的java实现。
 * 采用的是双向链表做的。
 */
package com.lish;

public class Josephus
{
	public static void main(String[] args)
	{
		CycLink cyclink=new CycLink();
		cyclink.Creat();
		cyclink.PrintAll();
		System.out.println();
		cyclink.DeleteChild();
		
	}
}

class Child
{
	int number;
	Child left;
	Child right;
	public Child(int number)
	{
		this.number=number;
	}
}

class CycLink
{
	Child firstChild;
	Child temp;
	Child temp1;//temp1,temp2的声明用于删除结点。
	Child temp2;
	
	//创建一个环形双向链表,指定人数为30人。
	public void Creat()
	{
		for(int i=1;i<=30;i++)
		{
			Child ch=new Child(i);
		//第一个孩子和最后一个孩子的情况要分开讨论。
			if(i==1)
			{
				this.firstChild=ch;
				this.temp=ch;
			}
			else
			{
				temp.right=ch;
				ch.left=temp;
				temp=ch;
				if(i==30)
				{
					temp.right=firstChild;
					firstChild.left=temp;
				}
			}
		}
	}
	
	//遍历打印所有结点
	public void PrintAll()
	{
		this.temp=firstChild;
		do
		{
			System.out.print(temp.number+" ");
			temp=temp.right;
		}while(temp!=firstChild);
	}
	
	//删除一个结点,并且打印出该结点,指定m=3。
	public void DeleteChild()
	{
		this.temp=firstChild;
	//当只剩下一个结点的时候就停止删除了,但是最后要将这个保留的结点一并打印出来。
		while(temp.right!=temp)
		{
			for(int i=1;i<3;i++)
			{
				temp=temp.right;
			}
			this.temp1=temp.left;
			this.temp2=temp.right;
			System.out.print(this.temp.number+" ");
			temp1.right=temp2;
			temp2.left=temp1;
			this.temp=temp2;			
		}
		System.out.print(this.temp.number+" ");	
	}
	
}


 

 

 

3.4 多态

1.多态性:访问子类可以通过访问父类:

Animal cat =new Cat();

Animal dog =new Dog();

2.在使用多态的时候,如果有使用覆盖函数,那么被覆盖的方法(即是父类中的的那个相应的方法)是要存在的。

3. 多态:一个引用(类型)在不同情况下的多种状态,可使代码更加灵活

4.java允许父类的引用变量引用它子类的实例,是自动完成的

package com.lish;
//改程序主要用于演示多态的使用。
public class DuoTai
{
	public static void main(String[] args)
	{
		Master master=new Master();
		Animal an=new Cat();
		Food f=new Fish();
		master.feed(an,f);
		
		
		an=new Dog();
		f=new Bone();
		master.feed(an, f);
	}
}

class Master
{
	public void feed(Animal animal,Food food)
	{
		animal.eat();
		food.getName();
	}
}

class Animal
{
	String name;
	int age;
	public void cry()
	{
		System.out.println("I don't know how to cry");
	}
	public void eat()
	{
		System.out.println("I don't know what to eat");
	}
}

class Cat extends Animal
{
	public void cry()
	{
		System.out.print("A cat crys like this:");
	}
	public void eat()
	{
		System.out.print("A cat likes eating ");
	}
}

class Dog extends Animal
{
	public void cry()
	{
		System.out.print("A dog crys like this:");
	}
	public void eat()
	{
		System.out.print("A dog likes eating ");
	}
}

class Food
{
	String name;

	public void getName()
	{
		System.out.println("No name");
	}
	
}

class Fish extends Food
{
	public void getName()
	{
		System.out.println("fish");
	}
}

class Bone extends Food
{
	public void getName()
	{
		System.out.println("bone");
	}
}


 

 

3.5  抽象类、接口(难点重点)

1.父类方法的不确定性,用抽象类修饰这个方法,abstract

2.抽象类还是可以一样被继承

3. 当一个类继承的类是抽象的类时候,就要把抽象类中的所有的抽象方法全部方法实现

4.abstract关键词来修饰的时候,一定是抽象类和抽象方法

5.在使用中不多,公司笔试的时候考很多

6.抽象类不能被实例化,只有被继承以后再去实例化

7.抽象类不一定要包含abstract方法,就算没有abstract方法,也不能实例化它

8.一旦类包含了abstract方法,这个类必须声明为abstract

9.抽象方法不能有主体“{}“

package com.lish1;


public class Abstract
{
	//Animal an=new Animal();
	//Because we cannot instantiate an abstract
	//class
}

//define an abstract class
abstract class Animal
{
	String name;
	int age;
	
	//Animal cries
	//abstract method can't be realized.
	//Abstract class needn't include the abstract method
	abstract public void cry();
	//Once there is an abstract,the class must be abstract.
	//no{}in the abstract method.
	//In the abstract class, instantiated method is allowed
	//public void cry(){}is allowed.
}
//when a class's father is an abstract class
//,it is required the realize all the abstract method
class Cat extends Animal
{
	//realize the super cry.
	public void cry()
	{
		//do nothing......is also realize
		System.out.print("Cats cry");
	}
}


 

 

10.接口就是给出一些没有内容的方法,封装到一起,到某个类要使用的时候,再根据具体情况把这些方法写出来,语法:

class 类名 implements 接口

{

        方法;

        变量;

}

11.接口不能被实例化

12.接口中的所有方法都不能有主体

13.抽象类里面是可以有实现了的方法的

14.接口中的所有方法都不能有主体,即都不能被实现

15.接口是更加抽象的抽象类!!!!

16.一个类继承抽象类或是使用接口,那么就要实现所有的抽象方法

17.一个类可以实现多个接口

18.接口中可以有变量(但是不能用privateprotected修饰)

19.接口中的变量本质上都是静态的,而且是final,不管你加不加static,所以可以直接使用:接口名.变量名

20. java开发中,经常把常用的变量定义在接口中作为全局变量使用

  访问形式:接口名.变量名

21.一个接口不能继承其它的类,但是可以继承别的接口

22.接口体现了程序设计的多态和高内聚低耦合的思想

package com.implement;
//interface 该程序用于演示接口。
public class Implement
{
	public static void main(String[] args)
	{
		System.out.print(Usb.a);
		Computer computer=new Computer();
		Camera camera=new Camera();
		Phone phone1=new Phone();
		computer.useUsb(camera);
		computer.useUsb(phone1);
	}
}

//USB interface

interface Usb
{
	int a=1;
	//define two methods
	//Notice
	//1.We can't instantiate an interface.
	//2.No function body.
	//3.In essence, the interface is static and final.
	//4.Interface can't extend other class.
	//5.Interface can extend other interface.
	/*
	 *
	 * 
	 */
	public void start();
	public void stop();
}
interface Mac
{
	public void cry();
}

//When a class instantiate an interface, 
//the class must instantiate all the methods in the interface
class Camera implements Usb,Mac
{
	public void start()
	{
		System.out.println("I am the camera,start!");
	}
	public void stop()
	{
		System.out.println("I am the camera,stop!");
	}
	public void cry()
	{
		
	}
}

class Phone implements Usb
{
	public void start()
	{
		System.out.println("I am the phone,start!");
	}
	public void stop()
	{
		System.out.println("I am the phone,stop!");
	}
}

//Computer

class Computer
{
	public void useUsb(Usb usb)
	{
		usb.start();
		usb.stop();
	}
}

3.6 final

1.实现接口和继承父类的区别:

2.java是单继承,一个类只允许继承一个父类,这种单继承的机制可以保证类的纯洁性,比C++的多继承机制简洁

3.实现接口可以看做是对单继承的一种补充

4.继承是层次式的,不太灵活,修改某个类就会打破这种继承的平衡,但是接口就不会,因为只针对实现接口的类才起作用

5.用接口体现多态:

6.前期绑定:在程序运行之前就进行绑定,由编译器和连接程序实现,又叫静态绑定,如static方法和final方法,包括private方法,它是隐式fi nal

7.后期绑定:在运行的时候根据对象的类型进行绑定,由方法调用机制实现,因此又叫动态绑定,或是运行时绑定,除前期绑定外的所有方法都属于后期绑定

8.final概念:final可以修饰变量和方法

当不希望父类的某些方法被子类覆盖的时,可以用final修饰

当不希望类的某个变量的值被修改,可以用final修饰

当不希望类被继承时,可以用final修饰

9.final修饰的变量一般用下划线书写

10.如果一个变量是final的,那么定义时候必须赋初值

11.final修饰的变量又叫常量,一般用XX_XX_XX命名

12.final什么时候用:

  处于安全的考虑,类的某个方法不允许修改

  类不会被其它的类继承

  某些变量值是固定不变的,比如pi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值