使用对象的步骤
创建对象
类名 对象名 = new 类名();
`School center = new School();
package com.company;
/**
* Created by ttc on 17-12-22.
*/
public class school {
String schoolName;
int classRoomName;
int latNname;
public void teach()
{
System.out.println("hello");
}
}
package com.company;
/**
* Created by ttc on 17-12-22.
*/
public class testSchool {
public static void main(String[] args) {
school sl=new school();
sl.teach();
sl.classRoomName=10;
sl.latNname=54;
System.out.println(sl.classRoomName);
System.out.println(sl.latNname);
}
}
image.png
定义类的方法
public 返回值类型 方法名() {
//这里编写方法的主体
}
两种情况
- 如果方法具有返回值,方法体中必须使用关键字return返回该值,返回值类型为该返回值的类型
return 表达式;
作用: 跳出方法、返回结果
public class Student{
String name = "张三";
public String getName(){
return name;
}
//……
}
- 如果方法没有返回值,返回值类型为void
public class Student{
String name = "张三";
public void getName(){
}
//……
}
一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段(10岁到60岁之间20元,否则免费)决定能够购买的门票价格并输出
public class JourneyPerson {
String name;
int age;
public void showInfo()
{
String ticketInfo = ""; //定义门票价格
if(age < 10 || age > 60)
{
ticketInfo = "门票免费";
}
else
{
ticketInfo = "门票价格为:20元";
}
System.out.println(name + "的年龄为:"+ age + ticketInfo);
public class Main {
public static void main(String[] args) {
// write your code here
JourneyPerson journeyPerson = new JourneyPerson();
while (true)
{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名,输入n退出程序");
//创建一个游人类
//将用户输入的姓名,赋值给游人对象journeyPerson的名字属性
journeyPerson.name = scanner.next();
if(journeyPerson.name.equals("n"))//代表用户想退出程序
{
// 退出程序
break;
}
//将用户输入的年龄,赋值给游人对象journeyPerson的年龄属性
System.out.println("请输入年龄");
journeyPerson.age = scanner.nextInt();
//输出该游人门票信息和个人信息showInfo
journeyPerson.showInfo();
}
System.out.println("退出程序");
package com.company;
/**
* Created by ttc on 2017/12/28.
*/
//private ,default, protected,public
public class Dog {
private String name = "旺财"; // 昵称
private int health = 100; // 健康值0---100 private私有的
private int love = 0; // 亲密度
private int type;//类型:1狗2企鹅
private int kind;//品种
public String toString()
{
String strKind = "";
if(kind == 1)
{
strKind = "拉布拉多";
}
else if(kind == 2)
{
strKind = "雪纳瑞";
}
String str = "宠物的自白,我的名字叫"
+name+"健康值是"+health+"和主人的亲密度是"+love+"我是一只"+strKind;
return str;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到宠物店");
System.out.println("请输入宠物名字");
String name = scanner.next();
System.out.println("请输入宠物类型:1狗,2企鹅");
int type = scanner.nextInt();
if(type == 1)
{
Dog d = new Dog();
System.out.println("请输入品种,1聪明的拉布拉多,2酷酷的雪纳瑞");
int kind = scanner.nextInt();
d.setKind(kind);
d.setName(name);
System.out.println(d);
}
else
{
//new 企鹅();
}
}
}