首页 > 代码库 > 第十一章
第十一章
(1)
1.万物皆对象:(地球上有什么?我们会不自觉的将地球上的事物归为具体类别)
2.对象:顾客 , 收银员;
官方定义:用来描述客观事,由一组属性和方法构成。
3.类就是对象的类型。
4.封装:封装就是把一事物包装起来和涵盖属性的方法。
5.类的命名规则:(1)不能使用java关键字。
(2)不能包含任何嵌入的空格或点号“.”,以及除下划线“——”,字符“$”外的特殊字符。
(3)不能以数字开头。
(1)
(1)
(1)
- (1) package 第十一章; public class Youxi { String name; String name1; String wu; String lei; int zhi; int li; public void show(){ System.out.println("我是英雄,我的基本信息如下:"+"\n"+"姓名:"+name+","+"生命值:"+zhi); System.out.println("我是武器,我的基本信息如下:"+"\n"+"武器名:"+wu+","+"攻击力"+li); System.out.println("我是怪物,我的基本信息如下:"+"\n"+"姓名:"+name1+","+"生命值:"+zhi+","+"类型:"+lei); } (2) package 第十一章; import java.util.Scanner; public class InitialYouxi { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Youxi a = new Youxi(); a.name = "李小侠"; a.name1 = "小龟"; a.wu = "死神镰刀"; a.lei = "潜水类"; a.zhi = 300; a.li = 12; a.show(); } }
package 第十一章; import java.util.Scanner; public class Visitor { String name; int age; public void show(){ Scanner input = new Scanner(System.in); while(!"n".equals(name)){ if(age>=18 && age<=60){ System.out.println(name+"的年龄为:"+age+"\t门票价格为:20元\n"); }else{ System.out.println(name+"的年龄为:"+age+"\t门票免费\n"); } System.out.print("请输入姓名:"); name = input.next(); if(!"n".equals(name)){ System.out.print("请输入年龄:"); age = input.nextInt(); } } System.out.print("退出程序!"); } } (2) package 第十一章; import java.util.Scanner; public class InitialVisitor { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Visitor xi = new Visitor(); System.out.print("请输入姓名:"); xi.name = input.next(); System.out.print("请输入年龄:"); xi.age = input.nextInt(); xi.show(); } }
public class Student { String name;//姓名 int age;//年龄 String classNo;//班级 String hobby;//爱好 //输出方法信息 public void show(){ System.out.println(name+"\n年龄:"+age+"\n班级:"+classNo+"\n爱好"+hobby); } } (2) package 第十一章; public class InitialStudent { public static void main(String[] args) { // TODO Auto-generated method stub Student student = new Student (); student.name ="张浩"; student.age = 10; student.classNo = "S1班"; student.hobby= "篮球"; student.show(); } }
package 第十一章; public class Password { String name; String mi; public void show(){ System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。"); } } (2) package 第十一章; import java.util.Scanner; public class InitialPassword { public static void main(String[] args) { Scanner input = new Scanner(System.in); Password a = new Password (); a.name = "admin"; a.mi = "111111"; System.out.println("请输入用户名:"); String name1 =input.next(); System.out.println("请输入密码:"); String mi1 = input.next(); if(a.name.equals(name1) && a.mi.equals(mi1)){ System.out.println("\n请输入新密码:"); a.mi=input.next(); System.out.println("修改密码成功,您的新密码为:"+a.mi); }else{ System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。"); } } }
- package 第十一章; public class Calculator { int a; int b; public void show(){ System.out.println("数字1:"+a+"数字2"+b); } } (2) package 第十一章; import java.util.Scanner; public class InitialCalculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); Calculator shu1 = new Calculator(); Calculator shu2 = new Calculator(); System.out.print("数字1:"); shu1.a=input.nextInt(); System.out.print("数字2:"); shu2.b=input.nextInt(); System.out.println(shu1.a+"-"+shu2.b+"="+(shu1.a-shu2.b)); System.out.println(shu1.a+"+"+shu2.b+"="+(shu1.a+shu2.b)); System.out.println(shu1.a+"*"+shu2.b+"="+(shu1.a*shu2.b)); System.out.println(shu1.a+"/"+shu2.b+"="+(shu1.a/shu2.b)); } }
- (1) package 第十一章; public class CurrentTime { int nian; int yue; int ri; int dian; int fen; String miao; public void show(){ System.out.println("时间是:"+nian+"年"+yue+"月"+ri+"日"+dian+"点"+fen+"分"+miao+"秒"); } public static void main(String[] args) { CurrentTime a = new CurrentTime(); a.nian =2015; a.yue =5; a.ri =12; a.dian =10; a.fen =11; a.miao ="00"; a.show(); } }
- (1) package 第十一章; public class Jishunji { String j1="CUP"; String j2="主板"; String j3="显示器"; String j4="硬盘"; String j5="内存"; public void showInfo(){ System.out.println("计算机的主要部件包括:"+j1+"\t"+j2+"\t"+j3+"\t"+j4+"\t"+j5+"等"); } public static void main(String[] args) { Jishunji a = new Jishunji(); a.showInfo(); } }
(1) package 第十一章; public class Jine { int fen; String name; public void show(){ System.out.println("积分:"+fen+","+"卡类型:"+name); } } (2) package 第十一章; import java.util.Scanner; public class InitialJine { public static void main(String[] args) { // TODO Auto-generated method stub Jine a = new Jine(); a.fen = 3050; a.name = "金卡"; a.show(); if(a.fen>1000&&a.name.equals("金卡")){ System.out.println("回馈积分500分!"); }else{ System.out.println("完"); } } }
- (1) package 第十一章; public class Teacher { String name; String n; String a; int nian; public void show(){ System.out.println(name+"\n专业方向:"+n+"\n教授课程:"+a+"\n教龄"+nian); } } (2) package 第十一章; public class InitialTeacher { public static void main(String[] args) { // TODO Auto-generated method stub Teacher shi = new Teacher(); shi.name = "王老师"; shi.n = "计算机"; shi.a = "使用java语言理解程序逻辑"; shi.nian = 5; shi.show(); } }
- (1) package 第十一章; /* * 管理员类 */ public class Administrator { String name; String mi; public void show(){ System.out.println("姓名:"+name+"\n"+"密码:"+mi); } } (2)package 第十一章; public class InitialAdministrator { public static void main(String[] args) { // TODO Auto-generated method stub Administrator a = new Administrator(); a.name = "黄振"; a.mi = "123456"; a.show(); } }
第十一章