您的位置:首页 > 编程语言 > Java开发

Java学习历程(Java基本语法演示)

2013-04-15 21:12 651 查看
/*---------------------数组模拟链表--数三退一-------------
*这里面为我所有学习JAVA时的例程,希望能给大家一点帮助.
*

 public class HelloWorld {
public static void main(String args[]) {
int length = 234;
int[] a = new int[length];

for(int i=0;i<length - 1;i++) {
a[i] = i + 1;
}

int count = 0 , index = 0 , oldIndex = 0;

while(length >1) {
count++;

if(count == 3) {
int i = index;
a[oldIndex] = a[index];
index = a[index];

a[i] = -1;
count = 0;
length --;

continue;
}

oldIndex = index;
index = a[index];
}

for(int i=0;i<a.length;i++) {
if(a[i] != -1) System.out.println(i);
}

}
}

----------------------	消费者和生产者		------------------
import java.util.*;

public class HelloWorld {
public static void main(String[] args) {
ManTouStack mts = new ManTouStack();
Consumer c = new Consumer(mts);
Producer p = new Producer(mts);
new Thread(c).start();
new Thread(p).start();

}
}

class  Consumer implements Runnable{
ManTouStack mts = null;
public void run() {
try{
for(int i=0;i<=30;i++) {
ManTou mt = new ManTou(i);
mts.pushManTou(mt);
System.out.println("做了一个馒头" + mt);
Thread.sleep((int)(Math.random() * 500));
}
} catch(InterruptedException ae) {
ae.printStackTrace();
}
}

public Consumer(ManTouStack mts) {
this.mts = mts;
}
}

class Producer implements Runnable {
ManTouStack mts = null;
public void run() {
try{
for(int i=0;i<30;i++) {
ManTou mt = mts.popManTou();
System.out.println("吃了一个馒头" + mt);
Thread.sleep((int)(Math.random() * 500));
}
} catch(InterruptedException ae) {
ae.printStackTrace();
}

}

public Producer(ManTouStack mts) {
this.mts = mts;
}
}

class ManTou {
int id;
public ManTou(int id) {
this.id = id;
}

public String toString() {
return "" + this.id;
}
}

class  ManTouStack {
static int index = 0;
ManTou[] mts = new ManTou[6];

public synchronized void pushManTou(ManTou mt) {

if(index == mts.length) {
try{
this.wait();
} catch(InterruptedException ae) {
ae.printStackTrace();
}
}
this.notifyAll();
mts[index] = mt;
index ++;
}

public synchronized ManTou popManTou() {
if(index == 0) {
try{
this.wait();
} catch(InterruptedException ae) {
ae.printStackTrace();
}
}
this.notifyAll();
index --;
return mts[index];
}
}

----------------------第十八个程序(多线程的练习)---------------
public class HelloWorld {
public static void main(String args[]) {
// TestThread1 tt = new TestThread1();
// tt.setPriority(6);			//设置线程优先级,等级为1-10,系统共有三个常量优先级MAX_PRIORITY=10;MIN_PRIORITY=1;NORM__PRIORITY=5;
// tt.start();
// try{
// tt.join();			//合并某一线程到正在执行线程中,正在执行线程需等合并线程执行返回方能继续执行
// } catch(InterruptedException ae) {
// ae.printStackTrace();
// }

// currentThread()		拿到当前线程对象
// isAlive()	判断线程是否存活

//Thread t = new Thread(tt);
//t.start();
// for(int i=0;i<=100;i++) {
// if(i % 5 == 0) {
// Thread.yield();			//该方法为该线程暂时停止运行
// }
// System.out.println("Main-" + i);

// }
// System.out.println(tt.getPriority());			//取进程优先级

DeathLock dl = new DeathLock();
dl.main();
}
}

class TestThread1 extends Thread implements Runnable {		//实现线程有两种方式,实现Runable接口或从Thread继承
public void run() {			//该方法为Thread执行主方法
for(int i=0;i<=100;i++) {
System.out.println("-------Thread-" + i);
}
}
}

class DeathLock implements Runnable {
int flag = 0;
static DeathLock o1 = new DeathLock() , o2 = new DeathLock();
public static void main() {
DeathLock dl = new DeathLock();
DeathLock dl2 = new DeathLock();
Thread t1 = new Thread(dl);
Thread t2 = new Thread(dl2);
t1.start();
dl2.flag = 1;
t2.start();
}

public void run() {
try {
if(flag == 0) {
synchronized(o1) {
System.out.println("o1已经锁定");
Thread.sleep(3000);
synchronized(o2) {
System.out.println("0");
}
}
}
if(flag == 1) {
synchronized(o2) {
System.out.println("o2已经锁定");
Thread.sleep(3000);
synchronized(o1) {
System.out.println("1");
}
}

}
} catch(InterruptedException ae) {
ae.printStackTrace();
}
}
}

-------------------------第十七个程序(IO类的演示)----------------------
import java.io.*;
public class HelloWorld {
public static void main(String args[]) {
FileByte fb = new FileByte();
TestInput ti = new TestInput();
TestObject to = new TestObject();
to.name = "诸葛亮";to.level = 21; to.death = true;
// to.outTestO();
// to.inTestO();
//fb.testFileByte();
//FileChar.testFileChar();
//BufferedIo.testBuffered();
//ti.testInput();

//TestPrint.testPrint();
TestPrint.testPrintFile(args[0]);
}

}

class FileByte {
public void testFileByte() {
try {
FileInputStream in = new FileInputStream("D:/javac/heLLoWorld.java");
FileOutputStream ou = new FileOutputStream("d:/Test/test.txt");
int count1,count2 = 0;
while((count1 = in.read()) != -1) {
ou.write(count1);
count2++;
System.out.print((char)count1);
}
System.out.println("共复制" + count2 + "个字节到测试文件....");
in.close();
ou.close();
} catch(FileNotFoundException ae) {
System.out.println("未找到文件...");
} catch(IOException ae) {
System.out.println("文件读写错误...");
System.exit(-1);
}

}

}

class FileChar {
public static void testFileChar() {
try {
FileReader in = new FileReader("D:/javac/HELLOWORLD.java");
FileWriter ou = new FileWriter("d:/test/test.dat");
int count1,count2 = 0;
while((count1=in.read())!= -1) {
ou.write(count1);
count2++;
System.out.print((char)count1);
}
System.out.println("共复制" + count2 + "个字节到测试文件....");
ou.close();
in.close();
} catch(FileNotFoundException ae) {
System.out.println("文件未找到....");
System.exit(-1);
} catch (IOException ae) {
System.out.println("文件读写错误...");
System.exit(-1);
}
}
}

class BufferedIo {
public static void testBuffered() {
int count;
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:/test/OutputStream.txt"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:/javac/HelloWorld.java"));
BufferedReader br = new BufferedReader(new FileReader("d:/javac/HelloWorld.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/test/BufferedReader.miz"));

OutputStreamWriter osw = new OutputStreamWriter(bos);		//输出字节流转字符流
InputStreamReader isr = new InputStreamReader(bis);			//输入字节流转字符流

// while((count=br.read()) != -1) {			//打印带缓冲区的字符
// System.out.print((char)(count));
// }

// while((count = isr.read()) != -1) {		//转换输入输出流大小
// System.out.print((char)(count));
// }
System.out.println(isr.getEncoding());

bos.close();bis.close();br.close();bw.close();osw.close();isr.close();

} catch(FileNotFoundException ae) {
System.out.println("文件未找到....");
System.exit(-1);
} catch(IOException ae) {
System.out.println("文件读写错误...");
System.exit(-1);
}
}

}

class TestInput {
public void testInput() {
try {
BufferedInputStream bos = new BufferedInputStream(System.in);
InputStreamReader isr = new InputStreamReader(bos);
BufferedReader is = new BufferedReader(isr);

String c = is.readLine();
while(c != null) {
if(c.toUpperCase().equals( "EXIT")) {
break;
} else {
System.out.println(c);
}
c = is.readLine();
}
is.close();isr.close();bos.close();
System.exit(-1);
} catch(IOException ae) {
System.out.println("文件读写错误...");
System.exit(-1);
}
}

}

class TestObject implements Serializable {	//想实现类的序列化(输出)必须实现该标志接口,否则报错
String name;							//Externalizable接口是控制序列化过程的接口,继承至Serializable
boolean death;
int level;
transient float bar  =(float) (2.132);		//透明的,即是在实现序列化是不保存该值
public  void outTestO() {
try {
FileOutputStream fod = new FileOutputStream("D:/test/testObj.dat");
ObjectOutputStream oos = new ObjectOutputStream(fod);
oos.writeObject(this);
oos.flush();
oos.close();fod.close();
} catch(FileNotFoundException ae) {
System.out.println("文件未找到....");
} catch(IOException ae) {
System.out.println("文件写出错误....");
}
}
public void inTestO() {
try {
FileInputStream fis = new FileInputStream("D:/test/testObj.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
TestObject a = (TestObject)(ois.readObject());
System.out.println("人物名字为:" + a.name + " 人物等级为:" + a.level + " 游戏进度为:" + a.bar + " 角色是否死亡:" + a.death);
fis.close();ois.close();
} catch(FileNotFoundException ae) {
System.out.println("文件未找到....");
} catch (ClassNotFoundException ae) {
System.out.println("角色读取失败...");
} catch(IOException ae) {
System.out.println("数据读入错误...");
}
}
}

class TestPrint {
public static void testPrint() {
try {
String s = null;
FileOutputStream fos = new FileOutputStream("D:/test/testprint.txt");
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.print("这是测试信息");
ps.flush();ps.c
e80f
lose();fos.close();
} catch(IOException ae) {
ae.printStackTrace();
}
}
public static void testPrintFile(String f) {
if(f != null) {testP(f,System.out);}

}
public static void testP(String s,PrintStream a) {
try {
FileReader fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
String s1 = "";
while(s1 != null ) {
s1 = br.readLine();
a.println(s1);
}
fr.close();
} catch(FileNotFoundException ae) {
ae.printStackTrace();
} catch(IOException ae) {
ae.printStackTrace();
}

}
}

-----------------------第十六个程序(容器的用法、应用)----------------
import java.util.*;
public class HelloWorld {
private static final Integer in = new Integer(1);
public static void main(String args[]) {
testWords(args);

}

public static void testWords(String[] s) {
Map m = new TreeMap();
for(int i=0;i<s.length;i++) {
Integer b =(Integer)m.get(s[i]);
m.put(s[i],b == null ? new Integer(1) : new Integer(b.intValue() + 1));
}
System.out.println(m.size() + " Words in the args");
System.out.println(m);
}
}

-----------------------第十五个程序(容器的自动解包、打包Auto-boxing/unboxing)------------------
import java.util.*;
public class HelloWorld {
public static void main(String args[]) {
Map m1 = new TreeMap();
Map m2 = new HashMap();
m1.put("one",new Integer(1));
m1.put("two",new Integer(2));
m1.put("three",new Integer(3));
m2.put("a",new Integer(1));
m2.put("b",new Integer(2));

// m1.put("one",1);
// m1.put("two",2);
// m1.put("three",3);
// m2.put("a",1);
// m2.put("b", 2);
System.out.println(m1.size());
System.out.println(m1.containsKey("two"));
System.out.println(m2.containsValue(new Integer("2")));
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);

}

}

-----------------------第十四个程序(容器的演示)-------------------
import java.util.*;
public class HelloWorld {
public static void main(String []args) {
Collection h = new HashSet();
HashMap M = new HashMap();
Set s = new HashSet();
List c = new ArrayList();
List li = new ArrayList();
List link = new LinkedList();

c.add(new String("aaa"));
c.add(new Long(123));
c.add(new Name("f1","n1"));
c.add(new Integer(234));

System.out.println("ArrayList取出内容:" + (Name)c.get(2));

System.out.println(" 遍历容器:" );
for(Object o : c) {				//遍历数组容器
System.out.print(" " + o);
}

link.add("a");link.add("b");link.add("c");link.add("d");
System.out.println("LinkedList取出内容:" + (String)link.get(0));

h.add(new Name("f2","n2"));
h.add(new Name("f1","n1"));
h.add(new Name("f3","n3"));
Iterator i = h.iterator();
//System.out.println(i.hasNext());

while(i.hasNext()) {			//移除内容
Name l = (Name)i.next();
if(l.equals(new Name("f2","n2"))){
i.remove();
}
}

System.out.println(h);
System.out.println(h.size());

int[] arr = {1 , 2 ,3 ,4 ,5};		//增强的for循环   jdk1.5以后才提供
for(int z : arr) {				//遍历数组
System.out.print(z);
}

}

}

class Name {
private String firstName , lastName;
public Name(String f , String l) {
this.firstName = f;
this.lastName = l;
}
public String getfirstName(){return firstName;}
public String getlastName() {return lastName;}
public String toString() {return firstName + " " + lastName;}
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name n = (Name)(obj);
return (n.firstName.equals(firstName)) && (n.lastName == this.lastName);
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
}

-----------------------第十三个程序(树状列文件目录演示、枚举类型演示)--------------
import java.io.*;			//引用
enum Mycolor {red , blue , green , bliack}			//定义一个枚举类型

public class HelloWorld {
public static void main(String[] args) {
Mycolor u = Mycolor.red;			//使用枚举类型
//Mycolor d = Mycolor.asdf;			//不存在此枚举,报错
File f = new File("d:/javac/mydir1");
file_list(f,0);
}
public static void file_list(File f , int l) {
File c[] = f.listFiles();

String s = "";
for(int i=0;i<l;i++) {
s = s + "   ";
}

for(int i=0;i<c.length;i++) {
if(c[i].isDirectory()  == true) {
l++;
System.out.println(s + c[i].getName());
file_list(c[i] , l);
} else {
System.out.println(s + c[i].getName());
}
}

}

}

--------------------第十二个程序(String File常用方法练习)-------------------
import java.io.*;
public class HelloWorld {
public static void main(String[] args) {
file_Demo();
}

public static void string_Demo() {
String a = "Hello Java",b = "Sun , MiscSoft , Unixc" ;
int c = 123456789;
// String d[] = a.split(",");		//以","分割字符串
// String e = String.valueOf(c);	//通过String.valueOf()将其转换为字符串,可转换所有类型(包括基本类型和引用类型)
// b.charAt(5);			//返回指定位置的字符
// b.length();			//返回字符串字符长度
// b.indexOf("M");			//返回指定字符串出现的第一个位置
// b.indexOf(3,"M");			//返回指定字符串从指定位置开始出现第一次的位置
// b.equalsIgnoreCase(a);			//比较两个字符串是否相同,忽略大小写
// b.replace("m","b");			//将替换所有目标字符
// b.startsWith("assd");			//判断是否以指定字符串开头
// b.endsWith("sdfsd");			//判断是否以指定字符串结尾
// b.toUpperCase();			//全转换为大写
// b.toLowerCase();			//
// b.substring(5);			//返回指定位置后的字符串
// b.substring(3,5);			//返回从指定位置开始到指定位置结尾的字符串
// b.trim();			//去掉字符串首尾空格
}

public static void file_Demo() {

// canRead()
// canWrite()
// exists()
// isDirectory()
// ifFile()
// isHidden()
// lastModified()
// length()
// getName()
// getPath()
// creatNewFile()
// delete()
// mkdir()
// mkdirs()
// getPatentFile()

String separartor = File.separator;
String filename = "myfile.txt";
String directory = "mydir1" + separartor + "mydir2";
//String directory = mydir1/mydir2;
File f = new File(directory,filename);
if(f.exists() == true) {
System.out.println("文件名:" + f.getAbsolutePath() + "\t文件大小:" + f.length() );
} else {
f.getParentFile().mkdirs();
try{
f.createNewFile();

} catch (IOException e) {
e.printStackTrace();
}
}
}
}

-----------------------第十一个程序(二维数组)------------------------------
public class HelloWorld {
public static void main(String args[]) {
String a = "1 , 2 ; 3 , 4 , 5 ; 6 , 7 ,8 ,9";

StringBuffer b = new StringBuffer("这是一个测试");
String[] c = a.split(";");
String[][] d = new String[c.length][];
for(int i=0;i<c.length;i++) {
d[i] = c[i].split(",");
}
System.out.println(d[1][2].trim());
}
}
----------————————---第十个程序(数组拷贝)-------------------------
public class HelloWorld {
public static void main(String []args) {
int []a = new int[5];
a[0] = 12 ; a[1] = 1 ; a[2] = 29 ; a[3] = 42 ; a[4] = 122 ;
int[] b = new int[6];
System.arraycopy(a,0,b,2,3);    参数:被拷贝数组 起始拷贝位置 拷贝至数组 起始拷贝位置 拷贝长度
System.out.println(b[1]);
}
}
---------------------第九个程序(查找方法练习 折半法)----------------------
public class HelloWorld {
public static void main(String args[]) {
int test[] = {1,4,6,8, 12,15,17,19,22,27,35,48,50,51,53,55,59,62};

System.out.print(middleSearch(test,19));
}
public static int middleSearch(int[] a ,int m) {
if(a.length == 0) return -1;
int start = 0 , end = 0 , middle = 0;
end = a.length - 1;
middle =(int)((start + end)/2);

while(start <= end) {
if(a[middle] == m) return middle;
if(a[middle] > m) {
end = middle - 1;
}
if(a[middle] < m) {
start = middle + 1;
}
middle =(int)((start + end)/2);
}
return -1;
}
}
----------------------第八个程序(数组的练习)------------------------
public class HelloWorld {
public static void main(String[] args) {
double[] a = {1.0,2.2,2.4,3.1};		//数组静态申明
sortarray(args);
num3quit.num3quit();
System.out.print(binarySearch(a,3));
}

public static void printarray(double[] b) {
for (int i=0;i<b.length;i++) {		//打印数组元素
System.out.println(b[i]);
}
}

public static void sortarray(String[] c) {
double d[] = new double[c.length];		//数组动态申明
for(int i=0;i<c.length;i++) {		//数组类型转换
d[i] = Double.parseDouble(c[i]);
}

for(int i=0;i<d.length;i++) {			//排序方法一  选择排序
for(int j=i;j<d.length;j++) {
double temp = 0;
if(d[i] > d[j]) {
temp = d[j];
d[j] = d[i];
d[i] = temp;
}
}
}

double temp;			//方法二   选择排序效率优化
int k;
for(int i=0;i<d.length;i++) {
k = i;
for(int j=k;j<d.length;j++) {
if(d[k] > d[j]) {
k = j;
}
}
if(k != i) {
temp = d[i];
d[i] = d[k];
d[k] = temp;
}
}
printarray(d);

double temp;			//冒泡排序法
for(int i=d.length-1;i>=0;i--) {
for(int j=0;j<i;j++) {
if(d[j] > d[j+1]) {
temp = d[j+1];
d[j+1] = d[j];
d[j] = temp;
}
}
}
printarray(d);
}
}

class num3quit {			//数组 数三退一算法,五百人,求最后留下人的编号
public static void num3quit() {
boolean[] pe = new boolean[500];			//定义五百人数组并初始化,用真假表示其是否还在数组中
for(int i=0;i<pe.length;i++) {
pe[i] = true;
}

int index = 0 , temp = 0 , num = pe.length;
while (num > 1) {			//判断循环直到只有最后一个剩余为止
if(index == pe.length) {
index = 0;
}
if(pe[index] == true) {
temp ++;
if(temp == 3) {
pe[index] = false;
temp = 0;
num --;
}
}
index++;
}
for(int i=0;i<pe.length;i++) {			//寻找出最后一个存在的人,并找出他的位置
if(pe[i] == true) {
System.out.println(i+1);
}
}
}

public static void num3quit_() {		//双向链表  数三退一算法,五百人,求最后留下人的编号

}
}

------------------------第七个程序(异常的捕捉及处理)--------------
public class HelloWorld {		//异常捕捉以及异常处理演示
public static void main(String args[]) {
int arr[] = {1 , 2 , 3};
try {
System.out.println(arr[1]);		//可出现数组异常错误,若出现错误后面语句不会执行
System.out.println(3/9);		//可出现除零异常,若出现错误后面语句不会执行
System.out.println(division(1,0));
} catch (ArithmeticException a){
System.out.println(a.getMessage());
System.out.println("出现除零错误...");
} catch (ArrayIndexOutOfBoundsException b) {
System.out.println("出现数组超界...");
b.printStackTrace();		//	printStackTrace() 打印出堆栈信息
b.getMessage();			//getMessage()取出错误信息
} finally {	//此关键字无论如何都被执行

}
}

public static double division(double div , double bediv) {
if(bediv == 0) {
throw  new ArithmeticException("被除数为零");	//申明抛出异常类型
}
return div/bediv;
}

public static void test() throws ArithmeticException {		//申明抛出异常,下面就可以直接调用不用异常处理了,但是调用这个方法的对象需要进行异常处理
division(3,1);
}

}
----------------------第六个程序(抽象类以及多态)-------------------
public class HelloWorld {
public static void main(String args[]) {
Student s = new Student("张瑞");
Testinterface t = new Student("王天");	//申明一个Testinterface的引用变量t来指向Student对象.
s.getName();
t.sing();
//t.getname();	//由于t是Testinterface引用变量,故没有getname方法
}
}

interface Testinterface {	//接口可以继承其他接口,并添加新的属性和方法
public static final int i = 5;	//接口默认申明属性为 public static final 也只能是public static final

public void sing();			//接口方法默认属性为public 也只能为public
public void sleep();
}

interface testinterface {
public void study();
public void sleep();
}

class Student implements Testinterface {	//申明引用Testinterface接口
private String name;

Student(String name) {
this.name = name;
}

public void study() {
System.out.println("学生学习...");
}

public void sing() {
System.out.println("学生唱歌...");
}

public void sleep() {
System.out.println("学生睡觉...");
}

public void getName() {
System.out.println("学生姓名:" + name);
}
}

--------------------第五个程序(final关键字)---------------

final class testfinal {	//此类不能被继承
public final int i = 8 ;	//此处i不能被修改

public void t(final int j) {}	//此处参数j一旦传值则不允许再修改

public final void m() {}	//此方法不能被重写
}
--------------------第四个程序(练习对象转型)------------

public class HelloWorld {
public static void main(String []args) {
CastingDog d1 = new CastingDog(1,2,8);
CastingDog d2 = new CastingDog(1,2,3);
System.out.print(d1.compare(d2));

}
}

class CastingDog {
public int length , wide , high;

public boolean compare(CastingDog a) {
if(a instanceof CastingDog) {
CastingDog v = (CastingDog)a;
if(length == v.length & wide == v.wide & high == v.high) {
return true;
}else return false;
}else{
return false;
}
}

CastingDog(int l , int w , int h) {
length = l;
wide = w;
high = h;
}
}

-------------------第三个程序(练习动态绑定和抽象类,共四个类)----------------------------
public class HelloWorld {
public static void main(String []args) {
Cat c = new Cat("bigcat" , "green");
Dog d = new Dog("cooldog" , "blue");
Lady l1 = new Lady("老公" , d);
Lady l2 = new Lady("老婆" , c);
l1.mypetenjoy();
l2.mypetenjoy();
}
}

abstract class Animal {	//由于类中有抽象方法,所以类必需申明为抽象类
private String name;

Animal() {}

Animal(String name) {
this.name = name;
}

//此程序段可用抽象方法实现
//public void enjoy() {
// 	System.out.println("叫声...");
//}

public abstract void enjoy();	//抽象方法,不需要实现,抽象方法必须被重写,抽象类必须被继承
}

class Cat extends Animal {
private String eyescolor;
public void enjoy() {
System.out.println("猫叫声....");
}
Cat(String name , String es) {
super(name);
this.eyescolor = es;

}
}
class Dog extends Animal {
private String furcolor;
public void enjoy() {
System.out.println("狗叫声....");
}
Dog(String name , String fc) {
super(name);
this.furcolor = fc;
}
}
class Lady {
private String name;
private Animal pet;
Lady(String name , Animal pet) {
this.name = name;
this.pet = pet;
}
public void mypetenjoy() {
pet.enjoy();
}
}

------------第二个程序(练习类的继承和构造方法的重载,共三个类)----------------------------------
class Teacher extends Person {
private String profession;

Teacher(String p) {
super ("zr");
profession = p;
}

public String info() {
return super.info() + " profession: " + profession;

}
}

class Person {
private String name;
private String location;

Person(String n) {
name = n;
location = "beijing";
}

Person(String n , String l) {
name = n;
location = l;
}

public String info() {
return "name:" + name + " location: " + location;
}
}

class Student extends Person {
private String school;

Student(String n , String s) {
this(n , s , "beijing");
}

Student(String name , String l, String school) {
super(name,l);
this.school = school;
}

public String info() {
return super.info() + " school: " + school;
}
}
-------------第一个程序(练习构造方法重载 , 共一个类)-----------------
class OnePoint {
double x,y,z;
public void setx(double _x){
x = _x;
}

public void sety(double _y) {
y = _y;
}

public void setz(double _z) {
z = _z;
}

public void juli(OnePoint e) {
System.out.print("\n\"p\"点= "+this.x+" "+this.y+" "+this.z);
System.out.print("\n\"p\"点= "+e.x+" "+e.y+" "+e.z);
System.out.print("\n距离= "+ ((x-e.x)*(x-e.x) + (y-e.y)*(y-e.y) + (z-e.z)*(z-e.z)));
}

public OnePoint(){}

public OnePoint(double _x ,double _y , double _z) {
z = _z;
x = _x;
y = _y;
}
}
*************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息