「java定义三个属性存储」java定义三个属性存储位置

博主:adminadmin 2022-12-05 00:12:07 56

本篇文章给大家谈谈java定义三个属性存储,以及java定义三个属性存储位置对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java编程,编写一个person类,包含名字(name),年龄(age),号码(number)三个属性。定义构造属性。

先定义一个Object数组 Object[] obj = new Object[10];

然后将生成的类放进数组中 obj[i]= new Person();

将数组写到txt文件中要用到对象输入输出流,最好给Person类重写toString方法

java定义一个类,每个三个属性,两个方法,方法中应用到定义的属性。

public class test(){

mian/+回车(我笔记本没编程工具,一般在公司才编程){

private string a;

private string b;

private string c;

按住shift+alt+s选择generategettersandsetters然后选择你要设置的属性自动生成get和set方法

}

}

JAVA定义一个类MyClass1,包含三个整型属性x,y,z和封装这个属性的两个方法getBest()和setData(int x,

public class MyClass1 {

private int x;

private int y;

private int z;

public void setData(int x, int y, int z){

this.x = x;

this.y = y;

this.z = z;

}

public int getBest(){

int result = 0;

if(xy){

if(xz){

result = x;

}else{

result = z;

}

}else{

if(yz){

result = y;

}else{

result = z;

}

}

return result;

}

public static void main(String[] args) {

MyClass1 class1 = new MyClass1();

class1.setData(10,13, 17);

System.out.println(class1.getBest());

}

}

java 写一个类Student,包含三个属性学号id,姓名name和年龄age;

public class Student {

public String id;

public String name;

public int age;

public Student(String id, String name, int age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

public Student(String id) {

super();

this.id = id;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (id == null) {

if (other.id != null)

return false;

} else if (!id.equals(other.id))

return false;

return true;

}

}

public class StuTest {

public static void main(String[] args) {

Student stu1=new Student("1234", "张三", 12);

Student stu2=new Student("1234");

System.out.println(stu2.equals(stu1));

}

}

用JAVA定义3个变量保存你的姓名,性别,及年龄

public class A{

//分别定义姓名:小强 性别:男 年龄:20岁

String name="小强";

String sex=男;

byte age=20;

}

Java存储对象到集合

package com.company;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

/**

* Created by hubin on 2018/8/10.

*/

public class Test {

public static void main(String[] args) {

ListStudent list = new ArrayList();

list.add(new Student("小赵",12,'男'));

list.add(new Student("小钱",15,'女'));

list.add(new Student("小孙",13,'男'));

IteratorStudent iter = list.iterator();

int maxAge = list.get(0).getAge(); //默认第1个年龄最大

//目标对象在list中的序号

int index = 0;

int i = 0; //计数,代表的是循环到了list的第几个对象

while (iter.hasNext()) {

Student stu = iter.next();

if(stu.getAge() maxAge) {

//如果哪个对象的年龄大于当前的最大年龄,更新最大年龄,并记下这个学生在list中的序号

maxAge = stu.getAge();

index = i;

}

i++;

}

//改名之前

System.out.println(list.get(index).getName() + "年龄最大");

list.get(index).setName("小猪佩奇");

//改名之后

System.out.println(list.get(index).getName() + "年龄最大");

}

}

class Student {

private String name;

private int age;

private char sex;

public Student(String name, int age, char sex) {

this.name = name;

this.age = age;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public char getSex() {

return sex;

}

public void setSex(char sex) {

this.sex = sex;

}

}

java定义三个属性存储的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java定义三个属性存储位置、java定义三个属性存储的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-05,除非注明,否则均为首码项目网原创文章,转载请注明出处。