前言:
此刻同学们对“java存储图片”大致比较注意,你们都想要剖析一些“java存储图片”的相关知识。那么小编同时在网上汇集了一些对于“java存储图片””的相关文章,希望姐妹们能喜欢,姐妹们快快来学习一下吧!/**
* 使用javabean和数组存储表格的信息
* 这是常用表格存储方式
* 通过类来规定表结构 通过类的对象代表每一行数据
*/
class Emp{
//employee雇员
private int id;
//属性常用私有private 通过public的get set方法调用
private String name;
private int age;
private boolean man;
private String date;
//通过类的属性规定表格的列 属性名字即表头
public Emp(){}
public Emp(int id, String name, int age, boolean man, String date) {
//alt+insert 生成constructor 构造器
this.id = id;
this.name = name;
this.age = age;
this.man = man;
this.date = date;
}
public int getId() {
//alt+insert 生产 getter and setter
return id;
}
public void setId(int 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;}
public boolean isMan() { return man; }
public void setMan(boolean man) { this.man = man; }
public String getDate() {return date;}
public void setDate(String date) {this.date = date;}
//只定义了私有属性和公共构造器、setget方法的类 javabean
@Override
public String toString() {
//重写toString
//将返回值类型改为void报错'toString()' in 'Emp' clashes with 'toString()' in 'java.lang.Object'; attempting to use incompatible return type
//方法重写的返回值类型要小于等于父类方法返回值类型
return ("["+getId()+", "+getName()+", "+getAge()+","+(isMan()?"Man":"Woman")+", "+getDate()+"]");
//养成调用属性全用get方法 不直接敲属性 的习惯 日后增删改属性的时候 可以通过改get set方法来让所有调用该属性的程序一起变更
//比如将getAge从int改为String 返回xx+"岁" age本身还是int 仍然可以数学运算 所有调用getAge的表格都更改为了 数字+"岁" 格式 更改类型很方便 也保护了原始数据
}
}
class Form{
public static void main(String[] args) {
Emp emp0 =new Emp(01,"Jack",18,true,"1991-1-1");
//每个对象对应表格的一行 对象每一列内容通过类的属性声明了类型和含义 具体属性的值通过构造器和set方法设定
Emp emp1 = new Emp(02,"Jenny",19,false,"1992-2-2");
Emp emp2 = new Emp(03,"Tom",20,true,"1993-3-3");
Emp[] emps = {emp0,emp1,emp2};
//用一维数组表示表格 每一个元素即对象对应表格的一行
for(int i = 0;i<emps.length;i++){
//遍历数组
if (emps[i].getAge()<19)continue;
//通过get方法调用私有属性age 判断小于19的对象跳过 因为调用属性age是int类型 和数字19比较无需转换类型
System.out.println(
"["+emps[i].getId()+
", "+emps[i].getName()+
", "+emps[i].getAge()+
", "+(emps[i].isMan()?"Man":"Woman")+
", "+emps[i].getDate()+"]");
//太长了换行
}
//通过重写toString来显示遍历信息
for (int i = 0 ; i<emps.length;i++){
if (emps[i].getAge()<19)continue;
System.out.println(emps[i]);
//打印对象时默认打印对象.toString()
//一般打印对象的目的就是打印对象包含的内容 通过更改默认的toString() 让for循环内的语句更直观更易懂
//通过for循环打印数组中每个元素的内容 把每个元素的内容写入toString()方法内 通过打印元素调用默认方法.toString
}
}
}
//练习 创建一个商品表格包含多种信息
class Info{
private int id;
private String name;
private String model;
private double price;
private double discount;
//name商品名 model型号 price价格 discount折扣
public Info(){}
public Info(int id, String name, String model, double price, double discount) {
this.id = id;
this.name = name;
this.model = model;
this.price = price;
this.discount = discount;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() {return name; }
public void setName(String name) {this.name = name; }
public String getModel() {return model;}
public void setModel(String model) {this.model = model; }
public double getPrice() { return price;}
public void setPrice(double price) {this.price = price; }
public double getDiscount() { return discount; }
public void setDiscount(double discount) {this.discount = discount;}
@Override
public String toString() {
//重写toString返回对象的所有信息
return "["+getId()+", "+getName()+", "+getModel()+", "+getPrice()+", "+getDiscount()+"]";
}
public String up(double p){
if (p<getPrice()*getDiscount()) {
//输入一个值,筛选出折后价大于该值的商品
return this+"\n";
//this即 this.toString()
}
return "";
}
}
class Form1{
public static void main(String[] args) {
Info in0 = new Info(1,"mouse","BZ",99.21,0.9);
Info in1 = new Info(2,"keyboard","WO",403.00,0.7);
Info in2 = new Info(3,"book","BK",89.00,0.8);
Info in3 = new Info(4,"cloth","GQ",700.00,0.5);
Info in4 = new Info(5,"phone","DM",900.00,0.3);
Info[] f = {in0,in1,in2,in3,in4};
for (int i = 0;i<f.length;i++){
//f.length 数组f的长度
System.out.println(f[i]);
}
Scanner s = new Scanner(System.in);
System.out.println("请输入价格,回车后显示折后价高于该价格的商品");
double s1 = s.nextDouble();
for (int i = 0;i<f.length;i++){
System.out.print(f[i].up(s1));
}
}
}
标签: #java存储图片