package cn.puruidong.openextends;/**
* @author PuRuidong
*
*/
/*
* 1.实现基本的注册选项;2.可继承(测试继承的特性)
* 包含功能:1.录入用户名(判断长度为4),2.录入生日(已添加验证方法),3.录入邮箱,(已添加验证方法)
* 包含:1.封装,2.可继承,3.多态
*/
import java.util.*;public class User {
private String id ;//录入用户名
private String birthday ;//录入生日
private String email ;//录入邮箱
private int indexof ;
private int lastindexof;
public String name="张三"; //公有,测试多态
Scanner sc = new Scanner (System.in);
//录入ID
public void setId (String id){
System.out.println("请输入您的用户名(4位以上):");
id=sc.next();
if (id.length()>4){
//输入正确,不予提示
}else{
System.out.println("长度错误,请重新输入.");
}
this.id=id;
}
public String getId(){
return id;
}
//录入生日
public void setBirthday (String birthday){
System.out.println("请输入您的生日(八位数字,形如:2000-05-01):");
birthday=sc.next();
indexof = birthday.indexOf("-");//正确情况下,indexof=4
lastindexof = birthday.lastIndexOf("-");//正确情况下,lastindexof=7
if (birthday.length()==10){//判断总体长度
if (indexof==4&&lastindexof==7&&indexof!=-1){//判断具体位置
//输入正确,不予提示
}else{
System.out.println("输入错误,请检查格式");
}
}else {
System.out.println("输入错误,请检查位数.");
}
this.birthday=birthday;
}
public String getBirthday (){
return birthday;
}
public void setEmail (String email){
System.out.println("请输入邮箱:");
email=sc.next();
indexof = email.indexOf("@");//检查@的位置
lastindexof = email.lastIndexOf(".");//检查.的位置
if (indexof!=-1&&indexof<lastindexof){//检查必须有@,而且.必须在@之后
//输入正确,不予提示
}else{
System.out.println("邮箱输入错误,请检查输入!");
}
this.email=email;
}
public String getEmail (){
return email;
}}
package cn.puruidong.Test;/**
* @author PuRuidong
*
*/
/*
* 显示继承,并且在衍生类中定义新属性
* 练习继承,对其中程序结构忽略,但“继承”已经可以正常使用
* 包含:1.封装,2.可继承,3.多态
* ..不在同一个包中
*/import cn.puruidong.openextends.*;
public class Register extends User { private int random ;//随机获得ID
public String name="李四"; //唯一公有的属性,测试多态
public void setRandom (int random){
random=(int)(Math.random()*1000);//随机获得ID数字
this.random=random;
}
public int getRandom (){
return random ;
}
public void show (){ //一系列调用
User center = new User ();
String id ="" ; //初始化
String birthday = ""; //初始化
String email = ""; //初始化
center.setId(id);
center.getId();
center.setBirthday(birthday);
center.getBirthday();
center.setEmail(email);
center.getEmail();
setRandom(random);
System.out.println("您的数字ID是:"+getRandom());
System.out.println("谢谢使用!");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Register s = new Register ();
User c = new Register ();//超类创建子类对象
s.show();
System.out.println(c.name); }}
请问,封装,继承,多态是这样理解的么?
* @author PuRuidong
*
*/
/*
* 1.实现基本的注册选项;2.可继承(测试继承的特性)
* 包含功能:1.录入用户名(判断长度为4),2.录入生日(已添加验证方法),3.录入邮箱,(已添加验证方法)
* 包含:1.封装,2.可继承,3.多态
*/
import java.util.*;public class User {
private String id ;//录入用户名
private String birthday ;//录入生日
private String email ;//录入邮箱
private int indexof ;
private int lastindexof;
public String name="张三"; //公有,测试多态
Scanner sc = new Scanner (System.in);
//录入ID
public void setId (String id){
System.out.println("请输入您的用户名(4位以上):");
id=sc.next();
if (id.length()>4){
//输入正确,不予提示
}else{
System.out.println("长度错误,请重新输入.");
}
this.id=id;
}
public String getId(){
return id;
}
//录入生日
public void setBirthday (String birthday){
System.out.println("请输入您的生日(八位数字,形如:2000-05-01):");
birthday=sc.next();
indexof = birthday.indexOf("-");//正确情况下,indexof=4
lastindexof = birthday.lastIndexOf("-");//正确情况下,lastindexof=7
if (birthday.length()==10){//判断总体长度
if (indexof==4&&lastindexof==7&&indexof!=-1){//判断具体位置
//输入正确,不予提示
}else{
System.out.println("输入错误,请检查格式");
}
}else {
System.out.println("输入错误,请检查位数.");
}
this.birthday=birthday;
}
public String getBirthday (){
return birthday;
}
public void setEmail (String email){
System.out.println("请输入邮箱:");
email=sc.next();
indexof = email.indexOf("@");//检查@的位置
lastindexof = email.lastIndexOf(".");//检查.的位置
if (indexof!=-1&&indexof<lastindexof){//检查必须有@,而且.必须在@之后
//输入正确,不予提示
}else{
System.out.println("邮箱输入错误,请检查输入!");
}
this.email=email;
}
public String getEmail (){
return email;
}}
package cn.puruidong.Test;/**
* @author PuRuidong
*
*/
/*
* 显示继承,并且在衍生类中定义新属性
* 练习继承,对其中程序结构忽略,但“继承”已经可以正常使用
* 包含:1.封装,2.可继承,3.多态
* ..不在同一个包中
*/import cn.puruidong.openextends.*;
public class Register extends User { private int random ;//随机获得ID
public String name="李四"; //唯一公有的属性,测试多态
public void setRandom (int random){
random=(int)(Math.random()*1000);//随机获得ID数字
this.random=random;
}
public int getRandom (){
return random ;
}
public void show (){ //一系列调用
User center = new User ();
String id ="" ; //初始化
String birthday = ""; //初始化
String email = ""; //初始化
center.setId(id);
center.getId();
center.setBirthday(birthday);
center.getBirthday();
center.setEmail(email);
center.getEmail();
setRandom(random);
System.out.println("您的数字ID是:"+getRandom());
System.out.println("谢谢使用!");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Register s = new Register ();
User c = new Register ();//超类创建子类对象
s.show();
System.out.println(c.name); }}
请问,封装,继承,多态是这样理解的么?
多态就是多种状态,User c = new Register ();之后c就可以调用Register的方法了,这就实现了多态。所以说,你的理解无误。