2020-12-04
  • |
  • daafoor
  • |
  • مشاهدات: 831

مثال بسيط على البرمجة الكائنية في جافا

object oriented example in java

 

class Book {
 private String name;
Author authors[];
 private double price;
 private int qty ;

 public Book(String name,Author authors[],double price,int qty){
this.name=name;
this.authors=authors;
this.price=price;
this.qty=qty;
}
public String getName(){
	return name;
	}
public Author[] getAuthors(){
	return authors;
	}
 public double getPrice(){
	return price;
}
public int getQty(){
	return qty;
}
public void setName(String name){
this.name=name;
	}
  public void setPrice(double price){
this.price=price;
	}
  public void setQty(int qty){
this.qty=qty;
	}

public void setAuthors(Author authors[]){
	this.authors=authors;
}

  public String toString()
  {
    String authors_list="";
    for(int i =0;i<authors.length;i++)
    {
      authors_list=authors_list+"\n"+authors[i].toString();
    }
    return "name: "+name+", price: "+price+", quantity: "+qty+"authors : "+authors_list;
  }
}


 class Author {
 private String name;
 private String email;
 private char gender;
public Author(String name,String email,char gender){
this.name=name;this.email=email;this.gender=gender;
}
public String getName(){
	return name;
	}
public String getEmail(){
	return email;
}
public char getGender(){
	return gender;
}
public void setName(String name){
  this.name=name;
	}
  public void  setEmail(String email){
this.email=email;
}
public void  setGender(char gender){
this.gender=gender;
}
    public String toString(){
  return name+" "+email+" "+gender;
}
 }


class Main {
  public static void main(String[] args) {
    Author author1=new Author(" hind ","hianada@gmail.com",'f');
Author author2=new Author("majeed "," majeed11@htmail.com",'m');
   System.out.println(author1.getName()+"    "+author1.getEmail()+"   "+author1.getGender());
   System.out.println(author2.getName()+"    "+author2.getEmail()+"   "+author2.getGender());
System.out.println(author1.toString());
System.out.println(author2.toString());
 Author a[]=new Author[2];
  a[0]=author1;
  a[1]=author2;

  Book b=new Book("intro to programming",a, 120, 3);
  System.out.println(b.toString());
    
  
  }
}

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول