第三次作业

本文通过三个Java类的实例化及属性设置演示了如何使用构造方法初始化对象,并展示了如何通过公共方法来获取和设置类的私有属性。

class Employee{
 int number;
 String name;
 int xinshui;
 int zengzhang;
 public void tell(){
  System.out.println("编号:"+number+",姓名:"+name+",薪水:"+xinshui+",增长:"+zengzhang+",工资:"+(zengzhang+xinshui));
 } 
}
public class dier{
 public static void main(String args[]){
  Employee per=new Employee();
  per.number=140201128;
  per.name="辽宁";
  per.xinshui=3000;
  per.zengzhang=1000;
  per.tell();
 }
}

 

 

class Dog{
 private String name;
 private String color;
 private int age;
 public Dog(String name,String color,int age){
  this.setName(name);
  this.setColor(color);
  this.setAge(age);
 }
 public void tell(){
  System.out.println("名字:"+name+",颜色:"+color+",年龄:"+age);
 }
 public String getName(){
  return name;
 }
 public void setName(String a){
  name=a;
 }
 public String getColor(){
  return color;
 }
 public void setColor(String b){
  color=b;
 }
 public int getAge(){
  return age;
 }
 public void setAge(int c){
  age=c;
 }
}
public class disi{
 public static void main(String args[]){
  Dog per=new Dog("大黄","黄",3);
  per.tell();
 }
}

 

 

class User{
 private String name;
 private String cdk;
 private int num;
 public User(){}
 public User(String name){
  this.setName(name);
 }
 public User(String name,String cdk){
  this.setName(name);
  this.setCdk(cdk);
 }
 public void tell(){
  System.out.println("名字:"+name+",口令:"+cdk+",个数:"+num);
 }
 public String getName(){
  return name;
 }
 public void setName(String a){
  name=a;
 }
 public String getCdk(){
  return cdk;
 }
 public void setCdk(String b){
  cdk=b;
 }
 public int getNum(){
  return num;
 }
 public void setNum(int c){
  num=c;
 }
}
public class diwu{
 public static void main(String args[]){
  User per=new User("LinXinHua","lxh",3);
  per.tell();
 }
}

 

class Address{
 String country;
 String shengfen;
 String city;
 String jiedao;
 int youbian;
 public void tell(){
  System.out.println("国家:"+country+",省份:"+shengfen+",城市:"+city+",街道:"+jiedao+",邮编:"+youbian);
 
}
public class diyi{
 public static void main(String args[]){
  Address per=new Address();
  per.country="中国";
  per.shengfen="辽宁";
  per.city="沈阳";
  per.jiedao="沈北新区";
  per.youbian=11300;
  per.tell();
 } 
}

 

转载于:https://www.cnblogs.com/hao520-/p/5336470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值