- package com.demo;
- public class Student {
- String name;
- char sex;
- int age;
- }
- package com.demo;
- public class StudentTest {
- public static void main(String[] args) {
- Student wangstu=new Student();
- wangstu.name="王洪涛";
- wangstu.age=18;
- wangstu.sex='女';
- wangstu.showStudent();
- }
- }
- package com.demo;
- import java.util.Scanner;
- public class PersonTest {
- public static void main(String[] args) {
- Person person=new Person();
- person.show();
- }
- }
protected: 受保护的
private: 私有的
public: 公共的
访问级别
- package com.demo;
- import java.util.Scanner;
- public class Person {
- public String name;
- public int age;
- /**
- * 用来循环接收用户输入, 然后判断年龄
- */
- /**
- * 如果类和属性还有方法不添加访问修饰符, 那么默认是?
- * 默认是 public
- */
- public void show(){
- Scanner input=new Scanner(System.in);
- System.out.println("请输入姓名:");
- name=input.next();
- //1. 循环
- while(!name.equals("n")){
- System.out.println("请输入年龄:");
- age=input.nextInt();
- //2. 判断
- if(age>=18&&age<=60){
- System.out.println("门票 20 元");
- }else{
- System.out.println("门票免费");
- }
- System.out.println("请输入姓名:");
- name=input.next();
- }
- System.out.println("程序结束!");
- }
- }
类与对象.
来源: http://www.bubuko.com/infodetail-2803775.html