重学JAVA基础(五):面向对象
1.封装
import java.util.Date;
public class Human {
protected String name;
protected BirthDay birthDay;
protected String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public BirthDay getBirthDay() {
return birthDay;
}
public void setBirthDay(BirthDay birthDay) {
this.birthDay = birthDay;
}
public int getAge(){//封装方法
if(birthDay!=null){
Date nowDate = new Date(System.currentTimeMillis());
int year = nowDate.getYear()+1900;
int month = nowDate.getMonth()+1;
int day = nowDate.getDate();
int age = year - this.birthDay.getYear();
if(this.birthDay.getMonth()>month){
return age-1;
}else if(this.birthDay.getMonth()==month){
if(this.birthDay.getDay()>=day){
return age-1;
}
}
return age;
}
return 0;
}
public String toString(){
return "name:"+name+" sex:"+sex+" age:"+getAge();
}
public String toString(Human human){
return this+"";
}
public Human(){
}
public Human(String name,String sex) {
super();
this.name = name;
this.sex = sex;
}
public Human(String sex) {
super();
this.sex = sex;
}
public Human(String name,String sex,int year,int month,int day) {
super();
this.name = name;
this.birthDay = new BirthDay(year, month, day);
this.sex = sex;
}
}
public class BirthDay {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public BirthDay(){
}
public BirthDay(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
}
2.继承
public class Woman extends Human{
public Woman(){
super("Woman");
}
public Woman(String name){
super(name,"Woman");
}
public String toString(){
return " I am Woman,"+super.toString();
}
public String toString(Woman woman){
return woman+"";
}
}
public class Man extends Human{
public Man(){
super("Man");
}
public Man(String name){
super(name,"Man");
}
public String toString(){
return " I am Man,"+super.toString();
}
public String toString(Man man){
return man+"";
}
}
3.多态
public class TestOO {
public static void main(String[] args) {
Human man = new Man("man");
Human woman = new Woman("woman");
System.out.println(woman.toString(man));
System.out.println(man.toString(woman));
}
}
运行结果:
I am Woman,name:woman sex:Woman age:0
I am Man,name:man sex:Man age:0
重学JAVA基础(五):面向对象的更多相关文章
- 重学JAVA基础(八):锁的基本知识
1.线程状态 如上图,当我们新建一个线程,并start后,其实不一定会马上执行,因为只有操作系统调度了我们的线程,才能真正进行执行,而操作系统也随时可以运行其他线程,这时线程又回到可运行状态.这个过程 ...
- 重学JAVA基础(二):Java反射
看一下百度的解释: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息 ...
- 重学JAVA基础(一):PATH和CLASSPATH
我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar 都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...
- 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep
/** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...
- 重学JAVA基础(六):多线程的同步
1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...
- 重学JAVA基础(四):线程的创建与执行
1.继承Thread public class TestThread extends Thread{ public void run(){ System.out.println(Thread.curr ...
- 重学JAVA基础(三):动态代理
1.接口 public interface Hello { public void sayHello(); } 2.实例类 public class Hello2 { public void sayH ...
- 12天,这本《重学Java设计模式》PDF书籍下载量9k,新增粉丝1400人,Github上全球推荐榜!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言
- 重学 Java 设计模式:实战抽象工厂模式
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- kotlin 语言入门指南(一)--基础语法
基于官网的Getting Start的基础语法教程部分,一共三节,这篇是第一节,翻译如下: 基础语法 定义一个包 包的声明必须放在文件头部: package my.demo import java.u ...
- 2009-04-19 22:40 SQL SERVER游标的讲解
游标和游标的优点 在数据库中,游标是一个十分重要的概念.游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结 果集中每次提取一条记录的机制.游标总 ...
- 对Oracle的rownum生成时机的理解
在Oracle中,rownum和rowid是平时经常用到的.比如rownum经常用于分页查询,rowid用于排重或者快速定位到记录. 对rownum跟order by配合下的生成时机一直没有具体研究过 ...
- Two stage U-Boot design
In AM335x the ROM code serves as the bootstrap loader, sometimes referred to as the Initial Program ...
- 1-3:CSS3课程入门之伪类和伪元素
E:target 表示当前的URL片段的元素类型,这个元素必须是E E:disabled 表示不可点击的表单控件 E:enabled 表示可点击的表单控件 E:checked 表示已选中的checkb ...
- 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016
一.Abstract 从近期对unsupervised learning 的研究得到启发,在large-scale setting 上,本文把unsupervised learning 与superv ...
- Android应用开发:网络工具——Volley(二)
引言 在Android应用开发:网络工具--Volley(一)中结合Cloudant服务介绍了Volley的一般使用方法.当中包括了两种请求类型StringRequest和JsonObjectRequ ...
- 关于打开sdk下载不了的最优秀解决方式
使用网站: mirrors.neusoft.edu.cn 东北大学就可以
- android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)
右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...
- 列举Python常用数据类型并尽量多的写出其中的方法
#1 把字符串的第一个字符大写 string.capitalize() #2 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 string.center(width) #3 返回 ...