一,继承
1,继承
一个父类可以有多个子类,而一个类只能有一个直接父类。子类可以访问父类的成员变量和成员方法,同hi也可以修改或重写父类的方法。通过继承可以实现代码复用。
2,举例应用
创建一个父类person,再创建一个子类Student继承父类person(使用toString和equals方法实现某些功能)
class Person{
private String name;
private char sex;
private int age;
public Person(String name,char sex,int age) {
this.name=name;
this.sex=sex;
this.age=age;
}
public Person() {
}
public String toString() {
return name+","+sex+","+age;
}
public boolean equals(Student student) {
if(this.name==student.getName() && sex==sex && age==age) {
return true;
}
else {
return false;
}
}
public String getName() {
return name;
}
public String getSex() {
return name;
}
public String getAge() {
return name;
}
}
class Student extends Person{
private long number;
private int phi,eng,comp;
public Student(String name,char sex,int age,long number,int phi,int eng,int comp) {
super(name, sex, age);
this.number=number;
this.phi=phi;
this.eng=eng;
this.comp=comp;
}
public Student() {
}
public double aver() {
return (phi+eng+comp)/3;
}
public int max() {
return phi>eng?(phi>comp?phi:comp):(eng>comp?eng:comp);
}
public int min() {
return phi<eng?(phi<comp?phi:comp):(eng<comp?eng:comp);
}
public String toString() {
return "学号:"+number+" "+"姓名:"+getName()+" "+"性别:"+getSex()+" "+"年龄:"+getAge()+" "+"平均分:"+aver()+" "+"最高分:"+max()+" "+"最低分:"+min();
}
public boolean equals(Student student) {
if(getName() == student.getName() && getSex()==student.getSex() && getAge() == student.getAge() && number==number) {
return true;
}
else {
return false;
}
}
}
public class SSS {
public static void main(String[] args) {
Person s1=new Student();
Person s2=new Student();
System.out.println(s1.equals(s2));
}
}
二,抽象类
1,抽象类即提供一个模板,根据他的格式去创建和修改新的类。
2,抽象类的举例应用
定义一个形状抽象类,根据他创建一个圆形子类和长方形子类,将其中的抽象方法(求面积 求体积 两个子类所共有的)在子类中实现,以供子类使用。
abstract class Shape{
protected String name;
public Shape(String name) {
this.name=name;
System.out.print("名称:"+name);
}
abstract public double getArea();
abstract public double getLength();
}
class Circle extends Shape{
private final double PI=3.14;
private double radius;
public Circle(String name,double radius) {
super(name);
this.radius=radius;
}
public double getArea() {
return PI*radius*radius;
}
public double getLength() {
return 2*PI*radius;
}
}
class Rectangle extends Shape{
private double length;
private double wide;
public Rectangle(String name,double length,double wide) {
super(name);
this.length=length;
this.wide=wide;
}
public double getArea() {
return length*wide;
}
public double getLength() {
return 2*(length+wide);
}
}
public class A {
public static void main(String[] args) {
Shape s1=new Circle("圆",10.2);
System.out.print("; 面积: "+s1.getArea());
System.out.println("; 周长: "+s1.getLength());
Shape s2=new Rectangle("长方形",6.5,10.3);
System.out.print("; 面积:"+s2.getArea());
System.out.println("; 周长:"+s2.getLength());
}
}
三,接口
1,接口可以实现多继承
2,接口的举例应用
定义一个形状类接口,以圆形类和长方形类来实现接口,并将接口中的抽象方法实现以供使用
interface IShape{
static final double PI=3.14;
double getArea();
double getLength();
}
class ICircle implements IShape{
private double radius;
public ICircle(double radius) {
this.radius=radius;
}
public double getArea() {
return PI*radius*radius;
}
public double getLength() {
return 2*PI*radius;
}
}
class IRectangle implements IShape{
private double length;
private double wide;
public IRectangle(double length,double wide) {
this.length=length;
this.wide=wide;
}
public double getArea() {
return length*wide;
}
public double getLength() {
return 2*(length+wide);
}
}
public class B{
public static void main(String[] args) {
IShape cir=new ICircle(5.0);
System.out.print("圆面积:"+cir.getArea());
System.out.println("; 圆周长:"+cir.getLength());
IShape rect=new IRectangle(6.5,10.8);
System.out.print("矩形面积:"+rect.getArea());
System.out.println("; 矩形周长:"+rect.getLength());
}
}
四,枚举
1,举例应用
定义一个表示一周七天的枚举,并在主方法中遍历枚举所有成员
enum Week
{MONDAY,TUESDAY,WEDNESDAY,THUSDAY,FRIDAY,SATURDAY,SUNDAY}
public class Person{
public static void main(String[] args) {
Week a=Week.MONDAY ;
Week b=Week.valueOf("TUESDAY");
System.out.print(a);
System.out.println(" "+b);
for(Week i:Week.values() ){
System.out.println("序号: "+ i.ordinal()+" "+i);
}
}
}
2,利用foreach语句遍历输出各元素
结构 for(type element:array) {
System.out.println(element);
..
..
}
举例 int[] arr= {1,2,3,4};
for(int element :arr){
System.out.println(element);
}