学习Java的日常
日期:2019-8-7
day3学习目的:完善美女选妃
学习接口,抽象类,继承,了解修饰符:protect private public defult;学习使用Scanner。
接口
interface A{//定义一个接口
public static final String MSG = "hello";//全局常量
public abstract void print();//抽象方法
}
抽象类
抽象类必须使用abstract修饰符来修饰,抽象方法也必须使用abstract修饰符来修饰,抽象方法不能有方法体。
抽象类不能被实例化,无法使用new关键字来调用抽象类的构造器创建抽象类实例。即使抽象类里不包含抽象方法,这个抽象类也不能创建实例。
抽象类可以包含成员变量、方法(普通方法和抽象方法都可以)、构造器、初始化块、内部类(接口、枚举)5种成分。
抽象类的构造器不能用于创建实例,主要用于被子类调用。
含有抽象方法的类(包括直接定义了一个抽象方法;或继承了一个抽象父类,但没有完全实现父类包含的抽象方法;或实现了一个接口,但没有完全实现接口包含的抽象方法三种情况)只能被定义成抽象类。
继承
继承在本职上是特殊一般的关系,即常说的is-a关系。子类继承父类,表明子类是一种特殊的父类,并且具有父类所不具有的 一些属性或方法。
1.Java继承的语法格式:
Java继承的关键字是:extends
public class 子类名 extends 父类名{…}
如: public class UNStudent extends Student {…} 注:
1.子类又称超类,拓展类 ;父类又称基类。
2.Java中类的继承只能是单继承(单根继承),即一个类只能继承一个父类,但是一个类可以由多个类来继承它。
3.Java会给每一个没有设置父类的类,自动添加一个父类就是Object 。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ListStudy {
int height1,height2,height3,weight1,weight2,weight3;
public ListStudy(int height1,int height2,int height3,int weight1,int weight2,int weight3)
{
this.height1=height1;
this.height2=height2;
this.height3=height3;
this.weight1=weight1;
this.weight2=weight2;
this.weight3=weight3;
}
public void getLady()
{
ArrayList <Integer> height=new ArrayList<>();
ArrayList<Integer>weight =new ArrayList<>();
height.add(height1);
height.add(height2);
height.add(height3);
weight.add(weight1);
weight.add(weight2);
weight.add(weight3);
System.out.println("身高数据"+height);
System.out.println("体重数据"+weight);
}
public Map<String,Integer> avg ()
{
int heighttotal=(height1+height2+height3)/3;
int weighttotal=(weight1+weight2+weight3)/3;
Map<String,Integer> map=new HashMap<>();
map.put("heights",heighttotal);
map.put("weights",weighttotal);
return map;
}
public boolean value(Map<String,Integer>Mode)
{
if(Mode.get("heights")<180&&Mode.get("heights")>160&&Mode.get("weights")<70&&Mode.get("heights")>50)
{
return true;
}
else
return false;
}
}
import java.util.*;
public class ListStudyTest {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int height1,height2,height3,weight1,weight2,weight3;
Map<String,Integer> map=new HashMap<>();
System.out.println("请输入三人身高");
height1=sc.nextInt();
height2=sc.nextInt();
height3=sc.nextInt();
System.out.println("请输入三人体重");
weight1=sc.nextInt();
weight2=sc.nextInt();
weight3=sc.nextInt();
ListStudy lady=new ListStudy(height1,height2,height3,weight1,weight2,weight3);
lady.getLady();
map=lady.avg();
String hw;
int avg;
System.out.println("请输入您要获取的平均值数据heights/weights");
hw=sc.next();
System.out.println(map.get(hw));
if(lady.value(map))
System.out.println("该组成员是美女");
else
System.out.println("该组成员不符合标准");
}
}