1.实现一个名为Person的类和它的子类Employee 属性用private来写
(1) Person类中的属性有:姓名name(String类型),年龄(int类型),性别(String类型);
(2) Employee类中的属性有:班级(String类型),成绩(double类型)编写这两个类。
(3) 定义一个测试类,给各个属性在构造函数中赋值后输出
package com.test0928;
public class Person {
private String name;
private int age;
private String sex;
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
public Person(){
}
}
package com.test0928;
public class Employee extends Person{
private String className;
private double score;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Employee(String name,int age,String sex,String className,double score){
super(name,age,sex);
this.className=className;
this.score=score;
}
}
package com.test0928;
import com.test0925.Person;
public class test01 {
public static void main(String[] args) {
Employee employee = new Employee("小红",22,"女","二班",98.5);
System.out.println("姓名:"+employee.getName()+","+"年龄:"+employee.getAge()+
",性别:"+employee.getSex()+",班级和分数分别是:"+employee.getClassName()+employee.getScore());
}
}
2.定义一个接口 Fly, 描述Fly的方法public void fly(),分别定义类人和鸟,实现Fly接口。定义一个测试类,测试人和鸟,在main方法中创建人对象和鸟对象,再定义一个makeFly()方法,其中让会飞的事物起飞。并在main方法中调用该方法。
package com.test0928;
public interface Fly {
public void fly();
}
package com.test0928;
public class People implements Fly {
@Override
public void fly() {
System.out.println("会飞");
}
}
package com.test0928;
public class Bird implements Fly {
@Override
public void fly() {
System.out.println("会飞1");
}
public void makeFly(){
System.out.println("起飞");
}
}
package com.test0928;
public class test02 {
public static void main(String[] args) {
People people =new People();
Bird bird = new Bird();
bird.makeFly();
System.out.println();
}
}
3.定义一个类,描述一个矩形,包含长、宽两种属性,和计算面积方法。编写一个类继承矩形类,该类描述长方体,具有长、宽、高属性,和计算表面积的方法。
编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其表面积。
package com.test0928_3;
public class Rectangle {
public int length;
public int weight;
public static void main(String[] args) {
}
public int area(int length,int weight){
this.length=length;
this.weight=weight;
System.out.println("面积是:"+length*weight);
return length*weight;
}
}
package com.test0928_3;
public class Cuboid extends Rectangle {
// public int length;
// public int weight;
public int height;
public int surfaceArea(int length,int weight,int height){
this.length=length;
this.weight=weight;
this.height=height;
System.out.println("表面积是:"+(length*weight+length*height+weight*height)*2);
return 2222;
}
}
package com.test0928_3;
public class test01 {
public static void main(String[] args) {
Cuboid cuboid =new Cuboid();
cuboid.surfaceArea(20,30,40);
cuboid.area(20,30);
}
}
4.在程序中写一个"HelloJavaWorld你好世界输出到桌面Hello.txt 文件中 并且 读取Hello.txt 文件中的内容 写入到 备份.txt 文件中
package com.test0928_4;
import java.io.*;
public class test01 {
public static void main(String[] args) throws IOException {
char[] arr = "HelloJavaWorld你好世界".toCharArray();
FileWriter out = new FileWriter(new File("C:\\Users\\Administrator\\Desktop\\Hello.txt"));
out.write(arr);
out.flush();
out.close();
FileReader in = new FileReader(new File("C:\\Users\\Administrator\\Desktop\\Hello.txt"));
FileWriter out1 = new FileWriter(new File("C:\\Users\\Administrator\\Desktop\\备份.txt"));
char[] arr1 = new char[1024];
int i = 0;
while ((i = in.read(arr1, 0, arr1.length)) != -1) {
out1.write(arr1);
out1.flush();
}
in.close();
out1.close();
}
}