KVC方法快速為資料對象賦值,kvc資料對象賦值
利用KVC方法快速為資料對象賦值,如下例
在.h檔案中聲明如下形式方法:
- (id)initWithDic:(NSDictionary *)dic;
在對應.m檔案中
- (id)initWithDic:(NSDictionary *)dic
{
self = [superinit];
if (self)
{
[selfsetValuesForKeysWithDictionary:dic];
}
returnself;
}
同時實現下面方法 ,實現內容一般不寫(必須實現,否則無法使用KVC)
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
Java中怎在for迴圈語句中對 類對象數組 進行賦值
比如有一個User類,他的類對象是user,這個類對象數組是
users [] 他的長度為10;
那麼你可以這樣對這個users通過對數組的遍曆,可用用for,也可以用foreach來做。
用for做就是:
for ( int i =0;i<users.length;i++){
users[i] ="這裡就是你要加的東西"+i; //這裡就是你想要加的任何東西
}
這樣就把這個類對象數組賦值完了
java中怎從檔案裡讀取資料,然後賦值給對象數組
大體的實現功能,代碼寫的並不嚴謹,很多需要判斷的地方都沒判斷,你湊合看看功能是如何?的就行,至於各種判斷驗證什麼的你自己搞定吧。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileToArray {
public static void main(String[] args) throws IOException {
String path = FileToArray.class.getResource("data.txt").toString()
.substring(6);
File file = new File(path);
BufferedReader in = new BufferedReader(new FileReader(file));
List<Student0> students = new ArrayList<Student0>();
String line;
int flg = 1;
while ((line = in.readLine()) != null) {
String[] data = line.split(" ");
String name = data[0];
long id = Long.parseLong(data[1]);
String[] result = new String[data.length - 2];
for (int i=2; i<data.length; i++) {
result[i-2] = data[i];
}
Student0 sdt = new Student0(name, id, result);
students.add(sdt);
}
in.close();
in = null;
file = null;
for (Student0 std : students) {
System.out.println(std.getName() + " " + std.getId());
for (String res : std.getResult()) {
System.out.println(res);
}
System.out.println("=================");
}
}
}
class Student0 {
private String name;
private long id;
private String[] result;
public Student0(String name, long id, String[] result) {
this.name = name;
this.id = id;
this.result = result;
}
public String getName() {
return name;
}
......餘下全文>>