仅供参考!
1.创建动物类
public class Animal {
public void eat() {
System.out.print("can eat 1 (1表示草,2表示肉)");
}
public void sleep() {
System.out.println(" also can sleep");
}
static class Rabbit extends Animal {
void Ra() {
System.out.print(" Rabbit");
eat();
sleep();
}
}
static class Tigger extends Animal {
public void eat(){
System.out.print("Tigger can eat 2 (1表示草,2表示肉)");
}
void Ti() {
System.out.print(" Tigger");
sleep();
}
}
}
class AnimalTest{
public static void main(String[] args) {
Animal.Rabbit a= new Animal.Rabbit();
a.Ra();
Animal.Tigger t= new Animal.Tigger();
t.eat(); t.Ti();
}
}
2.二维数组
public class Array2D {
public static void main(String[] args) {
int [][] twoD=new int[4][];
int a1[]=new int[4];
twoD[0]=a1;
int a2[]=new int[5];
twoD[1]=a2;
int a3[]=new int[6];
twoD[2]=a3;
int a4[]=new int[7];
twoD[3]=a4;
for(int i=0;i<twoD.length ;i++) {
for(int j=0;j<twoD[i].length ;j++) {
twoD[i][j]=(int)(Math.random()*100);
System.out.print(twoD[i][j]+"\t");
}
System.out.println();
}
int[] oneD={1,2,3,4};
twoD[0]=oneD;
for(int i=0;i<oneD.length ;i++) {
System.out.println(oneD[i]);
}
for(int i=0;i<twoD.length ;i++) {
for(int j=0;j<twoD[i].length ;j++) {
System.out.print(twoD[i][j]+"\t");
}
System.out.println();
}
}
}
3.创建电器类
import java.util.Scanner;
public class Electronics {
String P, U, I,AD_Type;
void Work() {
System.out.println("I'm working!!!");
}
static class Television extends Electronics {
String TV_Type,Max_volume;
Television(String P, String U, String I, String Max_volume, String TV_Type){
this.AD_Type=AD_Type;
this.P=P;
this.I=I;
this.U=U;
this.Max_volume=Max_volume;
}
@Override
void Work() {
super.Work();
System.out.println("I'm working hardly!!!"+"\n");
}
}
static class Refrigerator extends Electronics {
String Capacity;
Refrigerator(String P,String U,String I,String Caoacity){
this.Capacity=Caoacity;
this.P=P;
this.I=I;
this.U=U;
}
@Override
void Work() {
super.Work();
System.out.println("I'm working hardly vary much!!!");
}
}
public static class Test {
/*void f(Electronics n){
System.out.println("Please input electrical appliances: Refrigerator or Television:");
Scanner Elec=new Scanner(System.in);
if ()
}*/
public static void getPaly(Electronics t){
t.Work();
}
public static void main(String[] args) {
Television tv= new Television("2200W","220V","10A","100","海尔");
Refrigerator re=new Refrigerator("1100w","220v","5A","200L");
Electronics n=tv;
getPaly(n);
Electronics m=re;
getPaly(re);
}
}
}
4.捕获异常
class MyException extends Exception //自定义异常类
{public String toString()
{ return "除数等于零";
}
}
class DIV
{double DIV(double x,double y) throws MyException
{if(y==0) throw new MyException();
else return (x/y);
}
}
public class Ex16_2
{ public static void main (String[] args) throws MyException
{ double z;
DIV div=new DIV();
try{ z=div.DIV(100,0);
System.out.println ("当前z值:"+z);
}catch(MyException e)
{ System.out.println (e.toString());
}
}
}
5.教师、课程类
public class Henu {
String Tname;
String Major;
public void TeachWay() {
System.out.println("授课的步骤依次是:打开Eclipse、实施理论课授课");
}
public void Intrduce() {
System.out.println("My name is :" + Tname+"\n"+"My major is :"+Major);
}
static class JavaTe extends Henu {
void Java() {
Tname = "Zhang San";
Major = "Java";
TeachWay();
Intrduce();
}
}
static class NetTea extends Henu {
public void TeachWay(){
System.out.println("授课的步骤依次是:打开Visual Studio 2010、实施理论课授课");
}
void Net() {
Tname = "Li Si";
Major = "Net";
Intrduce();
}
}
}
class TeacherTest{
public static void main(String[]args){
Henu.JavaTe t =new Henu.JavaTe();
t.Java();
Henu.NetTea t2 =new Henu.NetTea();
t2.TeachWay(); t2.Net();
}
}
6.乐器类
public class Music_Instruments {
public static void main(String[] args) {
Violin p1 = new Violin();
testPlay(p1);
Piano p = new Piano();
testPlay(p);
}
public static void testPlay(Instrument n){
n.play();
}
}
abstract class Instrument {
abstract void play();
}
class Violin extends Instrument {
void play() {
System.out.println("小提琴 接着奏乐接着舞...");
}
}
class Piano extends Instrument {
void play() {
System.out.println("钢琴 接着奏乐接着舞...");
}
}
7.类方法计算长方体体积
class RectangleDemo
{ public static void main (String[] args)
{ double result;
Volume obj1=new Volume (10,20,30);
result=obj1.volume ();
System.out.println ("得到的长方体体积 ="+result);
result=obj1.area ();
System.out .println ("得到的长方形面积 ="+result);
result=obj1.volume (10.0);
System.out.println ("立方体的体积="+result);
}
}
class Rectangle
{ double length;
double width;
double area()
{ return length*width;
}
Rectangle(double w,double l)
{ length=l;
width=w;
}
}
class Volume extends Rectangle
{ double height;
Volume(double l,double w,double h)
{ super(l,w);
height=h;
}
double volume()
{ double vol;
vol=area()*height;
return vol;
}
double volume(double y)
{ return y*y*y;
}
}
8.数组的使用
import sun.awt.AWTAccessor;
public class Test14 {
public static void main(String[] args) {
int[] thisArray;
int[] thatArray;
int[] arr;
arr = new int[] {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
} ;
System.out.println("arr的初始值:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
thisArray = arr;
System.out.println("thisArray当前的值::");
for (int i = 0; i < thisArray.length; i++) {
System.out.print(thisArray[i] + "\t");
}
System.out.println();
System.out.println("输出thisAarray中数的阶乘:");
for (int i = 0; i < thisArray.length; i++) {
thisArray[i]=fac(thisArray[i]);
System.out.print(thisArray[i] + "\t");
}
thatArray=thisArray;
System.out.println();
System.out.println("再次输出thatAarray:");
for (int i = 0; i < thatArray.length; i++) {
System.out.print(thatArray[i] + "\t");
}
System.arraycopy(arr, 1, thatArray, 3, 2);
System.out.println();
System.out.println("复制后thatArray的值:");
for (int i = 0; i < thatArray.length; i++) {
System.out.print(thatArray[i] + "\t");
}
}
public static int fac(int n)
{
int f;
if(n==0||n==1)
f=1;
else
f=fac(n-1)*n;
return f;
}
}
class BasicArray{
}
9.Map存数据
import java.util.LinkedHashMap;
import java.util.Map;
public class Test15{
public static void main(String[] args) {
mapLoop();
}
// 定义一个MAP对循环写入(key为1到10,value为A到J),并循环输出
public static void mapLoop() {
// 有序排列
Map<String, String> map = new LinkedHashMap<>();
// 无序排列
// Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i <= 10; i++) {
map.put(String.valueOf(i), String.valueOf((char) (65 + i - 1)));
}
for (String s : map.keySet()) {
System.out.println("key: " + s + "\tvalue: " + map.get(s));
}
}
}
10.IO创建文件夹
public static void main(String[] args) throws IOException
{
File file = new File("E:/java/1.txt");
File file2 = new File("E:/java/异常1.jpg");
//File file3 = new File("E:/jav"); // 错误路径
System.out.println(file.createNewFile()); // 结果是false,已经存在无法创建
System.out.println(file2.createNewFile()); // 结果是false,已经存在无法创建
//System.out.println(file3.createNewFile()); // 结果是true,不存在可以创建
}