java学习第七天

来源:互联网 发布:unity3d 游戏菜单 编辑:程序博客网 时间:2024/04/30 00:56
/*
 * 作者:gnfire
 * 功能:丢手帕问题
 * 时间:20140226
 */
package com.gnfire;


public class Demo4 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CycLink cyclink=new CycLink();
cyclink.setLen(9);
cyclink.createLink();
cyclink.setK(2);
cyclink.setM(2);
cyclink.show();
cyclink.play();

}


}


class Child
{
int no;
Child nextChild=null;
public Child(int no){
//给一个编号
this.no=no;
}
}


//环形链表
class CycLink
{
//先定义一个指向链表第一个小孩的引用
//指向第一个小孩的引用,不能动
Child firstChild=null;
Child temp=null;
int len=0;//表示共有几个小孩
int k=0;
int m=0;


//设置链表大小
public void setLen(int len){
this.len=len;
}
//设置从第几个人开始数数
public void setK(int k){
this.k=k;
}
//设置m
public void setM(int m){
this.m=m;
}
//开始play

public void play()
{

Child temp=this.firstChild;

//1.先找到开始数数的人
for(int i=1;i<k;i++)
{
temp=temp.nextChild;
}
while(this.len!=1){
//2.数m下
for (int j=1;j<m;j++)
{
temp=temp.nextChild;
}
//找到要出圈的前一个小孩
Child temp2=temp;
while(temp2.nextChild!=temp){
temp2=temp2.nextChild;
}
//3.将数到m的小孩,退出圈
temp2.nextChild=temp.nextChild;
//让temp指向下一个数数的小孩
temp=temp.nextChild;
this.len--;
}
System.out.println("最后一个小孩是"+temp.no);
}

//初始化环形链表
public void createLink(){
for (int i=1;i<=len;i++){
if(i==1){
//创建第一个小孩
Child ch=new Child(i);
this.firstChild=ch;
this.temp=ch;
}else{
if(i==len){
//继续创建小孩
Child ch=new Child(i);
temp.nextChild=ch;
temp=ch;
temp.nextChild=firstChild;

}else{
//继续创建小孩
Child ch=new Child(i);
temp.nextChild=ch;
temp=ch;
}
}
}
}

//打印环形链表
public void show(){
//定义一个跑龙套
Child temp=this.firstChild;
do{
System.out.print(temp.no+" ");
temp=temp.nextChild;
}while(temp!=this.firstChild);

}


/*
 *作者:gnfire
 *功能:演示多态
 *时间:20140226
 *所谓多态,就是指一个引用(类型)在不同情况下的多种状态。
 *可以这样理解:多态是指通过指向父类的指针,来调用在不同子类中的实现方法。
 *多态注意事项:1.Java允许父类的引用变量引用它的子类的实例对象;Animal an=new Cat()
 *          这种转换是自动完成的
 *          2.还有其它要求,比如在某些情况下子类也可以转换为父类
 *          父类对象的引用可以指向子类的对象。
 *如Parent p=new Son()是可以的,反之不一定成立。
 *如果父类对象的引用指向的实际是一个子类的对象,那么父类对象的引用可以强制转化成子类对象的引用。如:
 *Parent p=new Son()
 *Son s=(Son)p;
 */
public class Demo5 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        
/* Cat cat=new Cat();
cat.cry();
Dog dog=new Dog();
dog.cry();
Animal animal=new Animal();
animal.cry();*/
//多态
/* Animal an=new Cat();//自动判断引用是什么
an.cry();
an=new Dog();
an.cry();*/
Master master = new Master(); 
master.feed(new Dog(), new Bone());
master.feed(new Cat(), new Fish());
}


}


//主人类
class Master{
//给动物喂食,使用多态,方法就一个就够了
public void feed(Animal an,Food f){
an.eat();
f.showName();
}

}


class Food{
String name;
public void showName(){

}
}


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

}


class Bone extends Food{
public void showName(){
System.out.println("骨头");
}

}
//动物类
class Animal{
String name;
int age;
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 void cry(){
System.out.println("Animal不知道怎么叫唤");
}
//动物可以吃
public void eat(){
System.out.println("不知道吃什么");
}
}
class Cat extends Animal{
//猫自己叫
public void cry(){
System.out.println("Cat喵喵叫");}
//猫吃
public void eat(){
System.out.println("猫喜欢吃鱼");
}
}
class Dog extends Animal{
//狗叫
public void cry(){
System.out.println("Dog汪汪叫");
}
//狗吃
public void eat(){
System.out.println("狗喜欢吃骨头");
}
}

0 0
原创粉丝点击