Java笔记1(java类、接口、封装、继承、多态、约瑟夫环应用)

[color=blue][size=x-large]文件结构 [/size][/color]
[img]http://images.cnblogs.com/cnblogs_com/luowei010101/201106/201106071458163814.png[/img]
[color=blue][size=x-large]Java访问修饰符 [/size][/color]
[img]http://images.cnblogs.com/cnblogs_com/luowei010101/201106/201106071458177029.png[/img]
com.xiaoming / XiaoMing.java: 

package com.xiaoming;
import java.util.*;
import com.xiaoqiang.*;
public class XiaoMing {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap hm=new HashMap();
Cat cat1=new Cat();
System.out.println(cat1.getName());
}
}

class Dog
{
}
com.xiaoqiang / Cat.java:

package com.xiaoqiang;

public class Cat {
public int a;
protected String name;
String color;
private float price;

//提供一个访问name的成员方法
public String getName()
{
return this.name;
}
}
com.xiaoqiang / Test2.java:

package com.xiaoqiang;

public class Test2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog1=new Dog();
System.out.println(dog1.a);
}
}

class Dog
{
public int a;
protected String name;
String color;
private float price;

public void abc1()
{
System.out.println(a);
}
}
Com.XiaoQiang /XiaoQiang.java:

package com.xiaoqiang;

public class XiaoQiang {

/**
* @param args
*/
public static void main(String[] args) {

}
}
com.test / Test.java:

/*
* 功能:抽象类的必要性
*/
package com.test;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
//Animal an=new Animal();//抽象类不能被实例化

}

}

abstract class Animal //抽象类
{
String name;
int age;
//动物会叫
/*
public void cry()//父类方法不确定性
{
System.out.println("不知道怎么叫!");
}
*/

public void aaa()
{
System.out.println("抽象类中的实现方法");
}
//抽象类中可以包含实现了的方法

abstract public void cry();//抽象方法
//抽象类的特征:
//1.抽象类不能被实例化
//2.抽象类可以没有抽象方法
//3.包含了abstract(抽象)方法的类,必须声明为abstract
//4.抽象方法不能有主体,即不能有{}
}

class Cat extends Animal
{
//当一个类继承的父类是抽象类的话,就需要在子类在把抽象类
//的所有抽象方法全部实现

//实现
public void cry()
{
System.out.println("猫猫叫。。。");
}
}
com.test2 / Test.java:

/*
* 功能:接口的案例
*/

package com.test2;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(Usb.a);

//创建一个计算机
Computer computer=new Computer();
//创建Camera
Camera cameral=new Camera();
//创建手机
Phone phone1=new Phone();

computer.useUsb(cameral);
computer.useUsb(phone1);

}
}

//Usb接口
interface Usb
{
int a=1;
public void start();
public void stop();
}
interface aaa
{
public void Eee();
}
interface kkk extends aaa
{
public void cry();
}

//接口不能被实例化,且接口中不能含有实现 了的方法
//接口可以理解成更加抽象的抽象方法
//接口中可以有被初始化的public,static,final类型的变量
//接口不能继承类,接口可以继承接口

//一个重要的原则:当一个类实现了一个接口,
//就要求该类把这个接口的所有方法都实现
class Camera implements Usb,kkk //一个类实现多个方法
{
public void start()
{
System.out.println("相机,开始工作了!");
}
public void stop()
{
System.out.println("相机,停止工作了!");
}
public void cry()
{

}
public void Eee()
{

}

}

class Phone implements Usb
{
public void start()
{
System.out.println("手机,开始工作了!");
}
public void stop()
{
System.out.println("手机,停止工作了!");
}
}

class Computer
{
//开始使用use接口
public void useUsb(Usb usb)
{
usb.start();
usb.stop();
}
}
com.test3 / Test.java:

/*
* final修饰类
*/
package com.test3;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

Aaa aaa=new Aaa();
aaa.show();
Bbb bbb=new Bbb();
bbb.show();
}
}

//final修饰类,则表示该类不能被继承
//final class Aaa
class Aaa
{
int a=0;//如果不给初值,a=? ,在类中的成员变量最好赋个初值
//圆周率不希望被别人改,加final修饰
//如果一个变量用final修饰,则在定义时必须被初始化,而且后面不能改
final float rate_aaa_bbb=3.1415926f;
//给方法用final修饰,则表示不可以被修改,不可被 覆盖
final public void sendMes()
{
System.out.println("发送消息");
}
public void show()
{
System.out.println("a="+this.a);
}
}
class Bbb extends Aaa
{
public Bbb()
{
a++;
//rate_aaa_bbb++; //因为rate_aaa_bbb为final类型
}
/*
public void sendMes()
{
System.out.println("发送消息");
}
*/
}

interface Fish
{
//该方法实现******
public void swimming();
}
interface Bird
{
public void fly();
}
class Monkey
{
int name;
//跳
public void jump()
{
System.out.println("猴子会跳");
}
}

class LittleMonkey extends Monkey implements Fish,Bird
{

@Override
public void swimming() {


}

@Override
public void fly() {

}
}
com..test3 / Mty99.java:

/*
99乘法表
*/

package com.test3;

public class Mty99
{
public static void main(String [] args)
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=i;j++)
{
if(i==3&&j==3||i==4&&j==3)
System.out.print(" "+j+"*"+i+"="+i*j+" ");
else
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println("");
}
}
}
com.luowei / Demo1.java:

/*
* 功能:说明继承的必要性
*/

package com.luowei;

public class Demo1 {

/**
* @param args
*/
public static void main(String[] args) {

Pupil p1=new Pupil();
p1.printName();
}
}

//小学生类
class Pupil extends Stu
{
/*
//定义成员属性
private int age;
private String name;
private float fee;
*/
//交学费
public void pay(float fee)
{
this.fee=fee;
}
}
//幼儿
class Kid extends Pupil
{
}

//中学生
class MiddleStu extends Stu
{
/*
//定义成员属性
private int age;
private String name;
private float fee;
*/
//交学费
public void pay(float fee)
{
this.fee=fee*0.8f;
}
}

//大学生生
class CloStu extends Stu
{
/*
//定义成员属性
private int age;
private String name;
private float fee;
*/
//交学费
public void pay(float fee)
{
this.fee=fee*0.1f;
}
}

//将学生的共同属性抽象出来,做一个父类
class Stu
{
protected int age;
public String name;
public float fee;

public void printName()
{
System.out.println("名字:"+this.name);
}
//如果不希望子类继承某个属性或方法,则将其显示声明为私有
}
com.luowei / Demo2.java:

package com.luowei;

import javax.swing.*;
public class Demo2 extends JFrame {
public static void main(String [] args)
{
//Demo2 demo2=new Demo2();

Abc abc1=new Abc();
System.out.println(abc1.getMax(4,1.2f));
}
/*
public Demo2()
{
this.setVisible(true);
this.setSize(200,200);
}
*/
}

class Abc
{
//返回较大的整数
public int getMax(int i,int j)
{
if(i>j)
{
return i;
}
else
{
return j;
}
}
public float getMax(float a,float b)//重载,参数类型不同
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
//如果只是返回类型不同,是不能构成重载的
/*
public double getMax(float a,float b)//重载,参数类型不同
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
*/

//如果只是控制访问修饰符号不一样,也不能构成重载
/*
protected float getMax(float a,float b)//重载,参数类型不同
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
*/
}
com.luowei / Demo3.java:

package com.luowei;

public class Demo3 {

public static void main(String [] args)
{
//创建一只猫
Cat cat1=new Cat();
cat1.cry();
Dog dog1=new Dog();
dog1.cry();
}
}

class Animal
{
int age;
String name;
//都会叫
public void cry()
{
System.out.println("是个动物,不知道 怎么叫唤");
}
}

//猫猫类
class Cat extends Animal
{
//覆盖就是将父类的方法再重新写一遍,如下边的cry()方法
public void cry()
{
System.out.println("猫猫叫!");
}
}

//狗类
class Dog extends Animal
{
public void cry()
{
System.out.println("汪汪叫!");
}
}
com.luowei / Demo4.java:

/*
* 功能:约瑟夫环,数到m的小孩 出圈
*/

package com.luowei;

public class Demo4 {

/**
* @param args
*/
public static void main(String[] args) {
CycLink cyclink=new CycLink();
cyclink.setLen(5);
cyclink.createLink();
cyclink.setK(2);//设置从第几个人开始数数
cyclink.setM(2);//设置的数m
cyclink.show();
cyclink.play();

}
}

class Child
{
int no;
Child nextChild;
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;
}

//设置m
public void setM(int m)
{
this.m=m;
}

//设置从第几个人开始数数
public void setK(int k)
{
this.k=k;
}

//开始数数
public void play()
{
Child temp=this.firstChild;
//1.先找到开始数数的人
for(int i=1;i<k;i++)
{
//因为自己数一下,再找到下一个人,所以将i<k
temp=temp.nextChild;
}

while(this.len!=1)
{
Child temp2=temp;//用来存放第m-1个孩子
//2.数m下
for(int j=1;j<m;j++)
{

if(j==m-1)
{
temp2=temp;
}
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=this.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);
}
}
com.luowei / Demo5.java:

/*
* 功能:多态的应用
*/

package com.luowei;

public class Demo5 {

/**
* @param args
*/
public static void main(String[] args) {
/*
Cat2 cat=new Cat2();
cat.cry();
Dog2 dog=new Dog2();
dog.cry();
*/
/*
Animal2 an=new Cat2();//多态的应用,创建Animal引用an,在这an是猫
an.cry();//通过Animal中的覆盖cry()方法可以实现多态
an=new Dog2();//利用多态性,使an的状态成为狗
an.cry();
//用一种引用可以管理很多的实例
*/
Master master=new Master();
master.feed(new Dog2(), new Bone());
master.feed(new Cat2(), new Fish());
}
}

//动物类Animal
class Animal2
{
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("不知道怎么叫");
}
//方法覆盖(重写)的作用:
//1.可以实现多态,实现一个父类引用管理多个子类实例,呈现多种状态
//2.可以具体化子类行为。

//动物可以吃东西
public void eat()
{
System.out.println("不知道 吃什么");
}
}

class Dog2 extends Animal2
{
//狗叫
public void cry()
{
System.out.println("汪汪叫");
}

//狗吃
public void eat()
{
System.out.println("狗喜欢吃骨头");
}
}

class Cat2 extends Animal2
{
//猫自己叫
public void cry()
{
System.out.println("猫猫叫");
}
//猫吃东西
public void eat()
{
System.out.println("猫喜欢吃鱼");
}
}

//食物
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 Master
{
//给动物喂食物,使用多态,方法就可以只用一个
//这样就不必给每一种具体类型的动物都写一个喂食物的方法了
public void feed(Animal2 an,Food f)
{
an.eat();
f.showName();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值