笔记07 类

类的框架结构: 由五部分组成(实例变量、构造器、设定器、访问器和功能方法)

 

封装一个类: Person,它用来描述和管理普通人的基本信息。

public class Person {
	
	//实例变量( 属性、字段、域 )
	private String name ;
	private boolean sex; //约定true表示男,false表示女
	private int age;
	
	//构造器( 类中的方法,其名称与类名完全相同且没有返回值的类型 )
	//作用: new + 构造器  实现对象的构造。
	public  Person(){
		
	}
	public  Person(String name){ //在构造对象的同时,完成对象name属性的初始化。
		this.name = name;
	}
	
	//设定器(调节器): 用来设置或修改对象的属性值。
	public void  setName( String name ){
		this.name = name; //形式参数name接收数据后,把它给实例变量name
	}	
	public void  setAge( int age ){
		this.age = age;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	
	//访问器: 用来获取对象的属性值。
	public  String  getName(){
		return name;
	}
	public  int  getAge(){
		return age;
	}
	public boolean getSex(){
		return sex;
	}
	
	//功能方法: 体现对象特有的能力,也体现对象的价值。
	public int add( int x, int y ){
		System.out.print(x + " + " + y + " = ");
		return x + y;
	}
	//将对象的信息以字符串方式反馈出去。
	public String getInfo(){
		return "姓名: " + name + "  性别: " + (sex ? "男" : "女") + "  年龄: " + age;
	}	
}


 

课堂练习:设计一个应用程序,通过类的方法来判断某年某月某日是当年的第几天

package work0903;

import java.util.Scanner;

public class Year1 {
	private int year;
	private int month;
	private int day;
	
	public Year1(){
		
	}
	public Year1(int year, int month, int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}
	//设定器
	public void setYear(int year){
		this.year = year;
	}
	public void setMonth(int month){
		this.month = month;
	}
	public void setDay(int day){
		this.day = day;
	}
	//访问器
	public int getYear(){
		return year;
	}
	public int getMonth(){
		return month;
	}
	public int getDay(){
		return day;
	}
	//功能方法
	public int date(){		
		System.out.print("你所给的年份是:" + year + "年, ");
		System.out.print("你所给的月份是:" + month + "月, ");
		System.out.print("你所给的日期是:" + day + "日, ");				
	int t = 0;
		if( year %4==0 && year %100!=0 || year %400==0){
				switch ( month ){
				case 1 : 	t = day; 	break;
				case 2 :	t = 31 + day;	break;				
				case 3 :	t = 31 + 29 + day; break;
				case 4 :	t = 31 + 29 + 31 + day; break;
				case 5 : 	t = 31 + 29 + 31 + 30 + day; break;
				case 6 : 	t = 31 + 29 + 31 + 30 + 31 + day; break;
				case 7 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + day; break;
				case 8 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; break;
				case 9 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; break;
				case 10 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break;
				case 11 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break;
				case 12 : 	t = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break;
				}	
		}else{
				switch ( month ){
				case 1 : 	t = day; 	break;
				case 2 :	t = 31 + day;	break;				
				case 3 :	t = 31 + 28 + day; break;
				case 4 :	t = 31 + 28 + 31 + day; break;
				case 5 : 	t = 31 + 28 + 31 + 30 + day; break;
				case 6 : 	t = 31 + 28 + 31 + 30 + 31 + day; break;
				case 7 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + day; break;
				case 8 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; break;
				case 9 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; break;
				case 10 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break;
				case 11 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break;
				case 12 : 	t = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break;
				}				
		}
		return t;		
	}


	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		
		System.out.println("请输入一个年份:");
		int a = s.nextInt();
		System.out.println("请输入一个月份:");
		int b = s.nextInt();		
		while(true){
			if(b >= 1 && b <= 12){
				break;
			}else{
				System.out.println("你输入的月份有误,请重新输入:");
				b = s.nextInt();
			}
		}
		System.out.println("请输入一个日期:");
		int c = s.nextInt();
		while(true){
			if(b==1 || b==3 || b==5 || b==7 || b==8 || b==10 || b==12){				
				if( c>=1 && c<=31){
					break;
				}else{
					System.out.println("你输入的日期有误,请重新输入:");
					c = s.nextInt();
				}					
			}else if(b==4 || b==6 || b==9 || b==11){
				if( c>=1 && c<=30){
					break;
				}else{
					System.out.println("你输入的日期有误,请重新输入:");
					c = s.nextInt();
				}
			}else{
				if(a%4==0 && a%100!=0 || a%400==0){
					if( c>=1 && c<=29){
						break;
					}else{
						System.out.println("你输入的日期有误,请重新输入:");
						c = s.nextInt();
					}
				}else{
					if( c>=1 && c<=28){
						break;
					}else{
						System.out.println("你输入的日期有误,请重新输入:");
						c = s.nextInt();
					}
				}
			}			
		}
		Year1 obj = new Year1(a, b, c);
		System.out.println( a + "年" + b + "月" + c +"日,是该年的第" + obj.date() + "天" );
	}
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值